In: Computer Science
Java Programming
Assignment 7.1 (25 points)
Using Java create an interactive (the user should be able to input the values) Java Application to process your utility bill for at-least 6 months. Use Class-Object methodology and use single dimensional arrays in your application. Write detailed comments in your program. You should have a two classes (for example, MontlyBill.java & MontlyBillTest.java). You can also be creative and create your “own problem scenario” and come up with your “solution”.
Assignment 7.2 (25 points)
Use the same program that you have created in 7.1, and use “two dimensional array”. Your program should display several months and several years. You can also be creative and create your “own problem scenario” and come up with your “solution”.
Step 1
Objective: This program would display the monthly bill amount for six months using class in Java. Here, the user inputs the expenditure (unit) and the rate of electricity consumption per unit is provided in the program.
Step 2
public class MonthlyBillClass {
public double billAmt;
public String monthData;
public int yearData;
public MonthlyBillClass(double billAmt, String monthData, int
yearData) {
this.billAmt = billAmt;
this.monthData = monthData;
this.yearData= yearData;
}
public void setBillAmount(double billAmt) {
this.billAmt = billAmt;
}
public double getBillAmount() {
return billAmt;
}
public void setMonth(String monthData) {
this.monthData = monthData;
}
public String getMonthName() {
return monthData ;
}
public void setYear(int yearData) {
this.yearData= yearData;
}
public int getYear() {
return yearData;
}
public String printData() {
return this.yearData + " " + this.monthData + " " +
this.billAmt;
}
}
Step 3
import java.util.Scanner;
public class MonthlyBillDemo{
public static void main(String[] args) {
int row = 6;
Scanner sc = new Scanner(System.in);
MonthlyBill[] monthlyBill = new MonthlyBill[row];
for (int i = 0; i < monthlyBill.length; i++) {
System.out.print("Enter month name : ");
String monthName = sc.next();
System.out.print("Enter year : ");
int year = sc.nextInt();
System.out.print("Enter bill Unit : ");
double billUnit = sc.nextDouble();
monthlyBill[i] = new MonthlyBill(billUnit, monthName,year);
}
System.out.println("\n" + "Utility bill payments ");
for (int i = 0; i < monthlyBill.length; i++) {
System.out.println( monthlyBill[i].printMonthData());
}
}
}
Step 4
Output: