Results 1 to 5 of 5
Thread: Use of Arrays to store data?
- 10-06-2010, 12:42 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 1
- Rep Power
- 0
Use of Arrays to store data?
Hi,
I completed a calculator for school which works fine.
However my teacher is saying i need to use Arrays to store data?
What do i need to modify?
Java Code:import java.text.DecimalFormat; import java.util.Scanner; public class HomeCalculator { public static void main(String[] args) { //added the mandatory class access specifier Scanner scanner = new Scanner(System.in); //i am referencing the scanner object String choice = null; DecimalFormat twoDForm = new DecimalFormat("#.##"); //for decimal formatting while (!"D".equals(choice)) { try { System.out .println("--------------------------------------------------------------------"); System.out.println("Welcome to John’s Home calculator"); System.out.println("Please select from the choices below"); System.out .println("--------------------------------------------------------------------"); System.out.println("Choice A: Rent Calculator"); System.out.println("Choice B: Electricity Bill Calculator"); System.out.println("Choice C: Loan Repayment Calculator"); System.out.println("Choice D: Quit calculator\n"); System.out.print("Choice: "); choice = scanner.next("[ABCD]"); //ensure only ABCD can be inputted if ("A".equals(choice) || "a".equals(choice)) { System.out .println("--------------------------------------------------------------------"); System.out.println("You have selected to use the Rent Calculator"); System.out.println("--------------------------------------------------------------------"); System.out.print("Please enter the weekly rent: "); double weeklyRent = scanner.nextDouble(); //reads the rate from input System.out.print("Please enter the number of students: "); int studentsResiding = scanner.nextInt(); System.out.println(); //using this statement to target one of multiple blocks of code to be executed if (weeklyRent <= 0) { System.out.println("Weekly rent amount cannot be zero or less."); } else if (studentsResiding <= 0) { System.out.println("Students sharing the rent cannot be zero or less."); } else { System.out.print("The monthly rent paid by each student is: $ "); double monthlyRentPerStudent = weeklyRent / studentsResiding; //weekly rent divided by amount of people System.out.println(twoDForm.format(monthlyRentPerStudent)); //output monthly rent per student } } else if("B".equals(choice)) { System.out .println("--------------------------------------------------------------------"); System.out.println("You have selected to use the Electricity Bill Calculator"); System.out.println("--------------------------------------------------------------------"); System.out.print("Please enter the initial meter reading: "); int initialReading = scanner.nextInt(); System.out.print("Please enter the final meter reading: "); int finalReading = scanner.nextInt(); int consumedUnits = finalReading - initialReading; double price = 0; if (consumedUnits < 0 || initialReading < 0) { System.out.println("\nPlease enter units above 0."); continue; } else if (consumedUnits <= 100) { price = consumedUnits * 0.5; } else if(consumedUnits > 100) { price = (( consumedUnits - 100 ) * 0.75) + 50; // where 50 is the price for the first 100 units } double billableAmount = price * 1.10; //where '.10' equals is the 10% GST System.out.println("\nThe electricity bill amount to be paid: $ " + twoDForm.format(billableAmount)); } else if("C".equals(choice)) { System.out .println("--------------------------------------------------------------------"); System.out.println("You have selected to use the Loan Repayment Calculator"); System.out.println("--------------------------------------------------------------------"); System.out.print("Please enter the loan amount: $ "); double billAmount = scanner.nextDouble(); System.out.print("No. of months for repayment: "); int months = scanner.nextInt(); System.out.print("Intrest Rate(1-100): "); double intrestRate = scanner.nextDouble(); System.out.println(); if(billAmount < 1500) { System.out.println("The minimum loan taken out must be greater than $1500."); } else if(months > 36) { System.out.println("The maximum repayment plan must be lower than 36 months (3 years)."); } else if(months < 1) { System.out.println("You cannot have less than 1 month!"); } else if(intrestRate > 100 || intrestRate < 0) { System.out.println("Incorrect interest rate. Correct interest input can range from 1-100."); } else { double repaymentAmount = (billAmount + (billAmount * intrestRate/100)) / months; //loan amount divide amount of months times interest rate equals repayments System.out.println("Repayment Amount per month: $ " + twoDForm.format(repaymentAmount)); } } System.out.println(); System.out.println(); } catch(Exception exp) //added a catch block to stop the program mishandling incorrect ABCD choices { System.out.println("--------------------------------------------------------------------"); System.out.println("Error has occured due to incorrect data input. Please try again."); System.out.println("--------------------------------------------------------------------\n\n"); scanner.next(); } } System.out.println("--------------------------------------------------------------------"); System.out.println("Thank you for using John’s Home calculator, G’day!"); System.out .println("--------------------------------------------------------------------"); scanner.close(); } }
- 10-06-2010, 01:08 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
No idea where you are supposed to be using an array in that lot.
There doesn't seem to be an obvious candidate to me.
- 10-06-2010, 09:29 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
If think your teacher is lazy to read your code, because it has much of the text.(long lines)
Places you may put data into array: messages like error... and ------ you can put into array and it would be more readable in the action code., also you can do as a constant 'CHOICE' and append the text to it. Try to work with your messages. But it's only my opinion,.
- 10-07-2010, 09:16 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
They wouldn't be arrays...static final, yes, but not arrays.
Anyway, you'd improve the clarity more by simply breaking that monster method in smaller ones. Even simply having each of the three options as a different method (or different class) would make mnore sense than employing arrays for the strings.
- 10-07-2010, 11:11 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Of course, in one aspect it's really hard to read. So that it's really difficult to track something in your code at once.
Secondly, it's bad coding practice, all about Object Oriented concepts. Keep your code is collection of smaller parts, know as functions mainly (involve some others too)
Similar Threads
-
Using Throwable to store data in buffers and "throwing" data
By BruteforceFtw in forum New To JavaReplies: 7Last Post: 06-08-2010, 02:02 PM -
Reading txt files and store them in arrays
By NeverHide in forum New To JavaReplies: 5Last Post: 05-20-2010, 08:18 PM -
Store values in arrays
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 1Last Post: 03-21-2010, 07:53 AM -
How to store data.
By djr1 in forum New To JavaReplies: 5Last Post: 02-17-2010, 07:33 AM -
how to store the data in data base
By eclipse3.4ide in forum New To JavaReplies: 5Last Post: 02-03-2009, 04:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks