In: Computer Science
Java Programming
Assignment 4.1
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 arraysin your application. Write detailed comments in your program. You should have a two classes (for example, MontlyBill.java & MontlyBillTest.java).
.Hint: Use the program (GradeBook) in Fig 7.14 & 7.15(GradeBookTest) as a building template.
Assignment 4.2
Use the same program that you have created in 4.1, and use “two dimensional array”. Your program should display several months and several years.
Hint: Use the program in 7.18 & 7.19 as templates.
Assignment 4.1 :
CODE :
File 1 : MonthlyBillTest.java
package monthlybilltest;
import java.util.Scanner;
public class MonthlyBillTest {
public static void main(String[] args) {
System.out.println("Enter monthly bills here for year 2018 :
");
Scanner sc = new Scanner(System.in);
MonthlyBill[] monthlyBill = new MonthlyBill[12];
double bill;
String monthName;
int year;
// enter details of month 1
System.out.print("Enter month name : ");
monthName = sc.next();
System.out.print("Enter bill amount : ");
bill = sc.nextDouble();
year = 2018;
monthlyBill[0] = (new MonthlyBill(bill, monthName, year));
// enter details of month 2
System.out.print("Enter month name : ");
monthName = sc.next();
System.out.print("Enter bill amount : ");
bill = sc.nextDouble();
year = 2018;
monthlyBill[1] = (new MonthlyBill(bill, monthName, year));
// enter details of month 3
System.out.print("Enter month name : ");
monthName = sc.next();
System.out.print("Enter bill amount : ");
bill = sc.nextDouble();
year = 2018;
monthlyBill[2] = (new MonthlyBill(bill, monthName, year));
// enter details of month 4
System.out.print("Enter month name : ");
monthName = sc.next();
System.out.print("Enter bill amount : ");
bill = sc.nextDouble();
year = 2018;
monthlyBill[3] = (new MonthlyBill(bill, monthName, year));
// enter details of month 5
System.out.print("Enter month name : ");
monthName = sc.next();
System.out.print("Enter bill amount : ");
bill = sc.nextDouble();
year = 2018;
monthlyBill[4] = (new MonthlyBill(bill, monthName, year));
// enter details of month 6
System.out.print("Enter month name : ");
monthName = sc.next();
System.out.print("Enter bill amount : ");
bill = sc.nextDouble();
year = 2018;
monthlyBill[5] = (new MonthlyBill(bill, monthName, year));
System.out.println("Printing all entered details : ");
// printing details of all 6 months :
for(int i=0; i<6; i++){
monthlyBill[i].printMonthData();
}
}
}
*******************************************************************
File 2 : MonthlyBill.java
package monthlybilltest;
public class MonthlyBill {
public double electricityBillAmount;
public String monthName;
public int year;
public double getElectricityBillAmount() {
return electricityBillAmount;
}
public void setElectricityBillAmount(double
electricityBillAmount) {
this.electricityBillAmount = electricityBillAmount;
}
public String getMonthName() {
return monthName;
}
public void setMonthName(String monthName) {
this.monthName = monthName;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public MonthlyBill(double electricityBillAmount, String
monthName, int year) {
this.electricityBillAmount = electricityBillAmount;
this.monthName = monthName;
this.year = year;
}
public void printMonthData(){
System.out.println(" "+ this.year + " " + this.monthName + " " +
this.electricityBillAmount);
}
}
----------------------------------------------------------------------------------------
Sample output :
-----------------------------------------------------------------------------------
Assignment 4.2 :
Code :
File 1 : MonthlyBillTest.java
package monthlybilltest;
import java.util.Scanner;
public class MonthlyBillTest {
public static void main(String[] args) {
System.out.println("Enter monthly bills here for year 2018 :
");
Scanner sc = new Scanner(System.in);
MonthlyBill[][] monthlyBill = new MonthlyBill[2][12];
// this array will store monthly bills.
// here row 1 will store the details of year 2017
// row 2 will store details of year 2018
double bill;
String monthName;
int year;
// enter details for year 2018 :
for(int i=0; i<2; i++){
System.out.print("Enter details for year "+((i == 0)? "2017" :
"2018") + "\n");
for(int j = 0; j<12; j++){
System.out.print("Enter month name : ");
monthName = sc.next();
System.out.print("Enter bill amount : ");
bill = sc.nextDouble();
if(i == 0){
year = 2017;
}
else{
year = 2018;
}
monthlyBill[i][j] = (new MonthlyBill(bill, monthName, year));
}
}
System.out.println("Printing all entered details : ");
// printing details of all 6 months :
for(int i=0; i<2; i++){
for(int j = 0; j<12; j++){
monthlyBill[i][j].printMonthData();
}
}
}
}
*********************************************************
File 2 : MonthlyBill.java
package monthlybilltest;
public class MonthlyBill {
public double electricityBillAmount;
public String monthName;
public int year;
public double getElectricityBillAmount() {
return electricityBillAmount;
}
public void setElectricityBillAmount(double
electricityBillAmount) {
this.electricityBillAmount = electricityBillAmount;
}
public String getMonthName() {
return monthName;
}
public void setMonthName(String monthName) {
this.monthName = monthName;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public MonthlyBill(double electricityBillAmount, String
monthName, int year) {
this.electricityBillAmount = electricityBillAmount;
this.monthName = monthName;
this.year = year;
}
public void printMonthData(){
System.out.println(" "+ this.year + " " + this.monthName + " " +
this.electricityBillAmount);
}
}
-------------------------------------------------------------------------
Sample output :
Enter monthly bills here for year 2018 :
Enter details for year 2017
Enter month name : jan
Enter bill amount : 1
Enter month name : feb
Enter bill amount : 2
Enter month name : march
Enter bill amount : 3
Enter month name : april
Enter bill amount : 4
Enter month name : may
Enter bill amount : 5
Enter month name : jun
Enter bill amount : 6
Enter month name : july
Enter bill amount : 7
Enter month name : august
Enter bill amount : 8
Enter month name : sept
Enter bill amount : 9
Enter month name : oct
Enter bill amount : 10
Enter month name : nov
Enter bill amount : 11
Enter month name : dec
Enter bill amount : 12
Enter details for year 2018
Enter month name : jan
Enter bill amount : 1
Enter month name : feb
Enter bill amount : 22
Enter month name : march
Enter bill amount : 33
Enter month name : april
Enter bill amount : 44
Enter month name : may
Enter bill amount : 55
Enter month name : jun
Enter bill amount : 66
Enter month name : july
Enter bill amount : 77
Enter month name : aug
Enter bill amount : 88
Enter month name : sept
Enter bill amount : 99
Enter month name : oct
Enter bill amount : 1010
Enter month name : nov
Enter bill amount : 1111
Enter month name : dec
Enter bill amount : 1212
Printing all entered details :
2017 jan 1.0
2017 feb 2.0
2017 march 3.0
2017 april 4.0
2017 may 5.0
2017 jun 6.0
2017 july 7.0
2017 august 8.0
2017 sept 9.0
2017 oct 10.0
2017 nov 11.0
2017 dec 12.0
2018 jan 1.0
2018 feb 22.0
2018 march 33.0
2018 april 44.0
2018 may 55.0
2018 jun 66.0
2018 july 77.0
2018 aug 88.0
2018 sept 99.0
2018 oct 1010.0
2018 nov 1111.0
2018 dec 1212.0
BUILD SUCCESSFUL (total time: 1 minute 45 seconds)