/** * Write a complete Java program to compute the total living cost for a semes
ID: 3697932 • Letter: #
Question
/** * Write a complete Java program to compute the total living cost for a semester at college. * The main method calls three methods to do this: 1. getTotalWeeks: requests from the user the total number of weeks they will attend college * and returns this number as an integer. 2. getDailyExpenses: requests from the user the estimated daily living expenses for each day * of the week (seven values) and returns these as an array of doubles. 3. computeLivingExpenses: takes as parameters the total number of weeks * (the integer value returned from the first method) and the estimated * daily expenses (array of seven doubles returned from the second method) and * returns a double representing the estimated total living expenses for the semester. * To compute the total living expenses, use a for loop to first compute the total weekly living expenses * then multiply this number by the total number of weeks the student will be in college. * */
Explanation / Answer
TotalLivingCost.java
package com.chegg.questions;
import java.util.Scanner;
public class TotalLivingCost {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numOfWeeks = getTotalWeeks();
double[] dailyExpense = getDailyExpenses();
double totalLivingCost = computeLivingExpenses(dailyExpense, numOfWeeks);
System.out.println("Total Living cost:" + totalLivingCost);
sc.close();
}
private static double computeLivingExpenses(double[] dailyExpense,
int numOfWeeks) {
double totalLivingCost = 0;
double weeklyExpense = 0;
for (int i = 0; i < dailyExpense.length; i++) {
weeklyExpense += dailyExpense[i];
}
totalLivingCost = weeklyExpense * numOfWeeks;
return totalLivingCost;
}
private static double[] getDailyExpenses() {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int j = 1;
double[] dailyExpenses = new double[7];
System.out
.println("please enter the estimated daily living expenses for each day");
for (int i = 0; i < dailyExpenses.length; i++) {
System.out.println("Expense of Day " + j);
dailyExpenses[i] = sc.nextDouble();
j++;
}
return dailyExpenses;
}
private static int getTotalWeeks() {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
int numOfWeeks = 0;
System.out.println("Please enter number of weeks you attend college");
numOfWeeks = sc.nextInt();
return numOfWeeks;
}
}
Output:
Please enter number of weeks you attend college
2
please enter the estimated daily living expenses for each day
Expense of Day 1
120
Expense of Day 2
100
Expense of Day 3
80
Expense of Day 4
100
Expense of Day 5
70
Expense of Day 6
100
Expense of Day 7
50
Total Living cost:1240.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.