Question: In Java, Using the program below program, create a two-dimensional array. The program should display several months and several years. Here is my code with
In Java, Using the program below program, create a two-dimensional array. The program should display several months and several years.
Here is my code with a single array, I can not figure out how to turn this into a double array:
MonthlyBillTest.java
import java.util.Scanner;
public class Assign7_1_MonthlyBillsTest {
public static void main(String[] args) { String months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; // months array Scanner input = new Scanner(System.in); System.out.println("Please enter your account number: "); //prompt Assign7_1_MonthlyBills.accountNumber = input.nextInt(); // user input System.out.println("Please enter the year of the records you would like to access: "); // prompt Assign7_1_MonthlyBills.year = input.nextInt(); // user input System.out.println("Please enter the month that you like to enter your bill amount " + " (January, February, March, April, May, June, July, August, September, " + " October, November, or December): " ); // prompt String start = input.next(); // user input int startIndex = getIndex(start, months); double bills[] = new double[12]; for(int i = startIndex; i < months.length; i++) { System.out.print("Please enter your bill amount for " + months[i] + ": "); // prompt with the following month bills[i] = input.nextDouble(); // user input } System.out.printf(" Utility bill for " + Assign7_1_MonthlyBills.year + " for account number: " + Assign7_1_MonthlyBills.accountNumber + " "); for(int i = startIndex; i < months.length; i++) { System.out.println(months[i] + ": $" + bills[i]); } } private static int getIndex(String start, String[] months) { for(int i = 0; i < months.length; i++) if(months[i].equalsIgnoreCase(start)) return i; return 0; } }
MonthlyBill.java
public class Assign7_1_MonthlyBills {
String arr[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; static int accountNumber; // the users account number String months; static int year; // the year the user want to add their bills in double billAmount; // constructor public Assign7_1_MonthlyBills(int accountNumber, String months) { Assign7_1_MonthlyBills.accountNumber = accountNumber; this.months = months; } public int getAccountNumber() { return accountNumber; }
public void setAccountNumber(int accountNumber) { Assign7_1_MonthlyBills.accountNumber = accountNumber; } // method to set the month public void setMonth(String months) { this.months = months; } // method to retrieve the month public String getMonth() { return months; } public double getBillAmount() { return billAmount; }
public void setBillAmount(double billAmount) { this.billAmount = billAmount; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
