I keep getting this error when trying to determine average grade, - java

double average = calcAverage(grade1, grade2, grade3, grade4, grade5);
System.out.println("The average is: ");
determineGrade(average);
public static double calcAverage(int grade1, int grade2, int grade3, int grade4, int grade5) {
double average = (grade1 + grade2 + grade3 + grade4 + grade5) / 5;
return average;
}
I keep getting the error where it says determineGrade(average), The error says:
This method must return a result of type double

All your inputs are ints. So your result will be an int. You need to cast at least one int to double then your result would be a double as well.
double average = (double) (grade1 + grade2 + grade3 + grade4 + grade5) / 5

That's because in determineGrade you return nothing. You only print something, but return nothing. So, please make the return type of determineGrade void, or, at least make it return something in double.

Ok sorry, but the rest of the code comes as follows,
import java.util.*;
public class Stock
{
public static void main (String[] args)
{
int grade1=0;
int grade2=0;
int grade3=0;
int grade4=0;
int grade5=0;
// All of the below are gathering input from user
System.out.println("What is the first grade?");
Scanner input = new Scanner(System.in);
grade1=input.nextInt();
System.out.println("What is the second grade?");
Scanner input2 = new Scanner(System.in);
grade2=input.nextInt();
System.out.println("Whar is the third grade?");
Scanner input3 = new Scanner(System.in);
grade3=input.nextInt();
System.out.println("What is the fourth grade?");
Scanner input4 = new Scanner(System.in);
grade4=input.nextInt();
System.out.println("What is the fifth grade?");
Scanner input5 = new Scanner(System.in);
grade5=input.nextInt();
double average = calcAverage(grade1, grade2, grade3, grade4, grade5);
System.out.println("The average is: ");
determineGrade(average);
}
public static double calcAverage(int grade1, int grade2, int grade3, int grade4, int grade5)
{
double average = (grade1 + grade2 + grade3 + grade4 + grade5) / 5;
return average;
}
public static double determineGrade(double average)
{
if (average>90)
{
System.out.println("You got an A");
}
else if (average>=80)
{
System.out.println("You got a B");
}
else if (average>=70)
{
System.out.println("You got a C");
}
else if (average>=60)
{
System.out.println("You got a D");
}
else if (average<60)
{
System.out.println("You got an F");
}
}
}

Related

How to cause an error message when a negative number is inputted?

I wrote the code to calculate the total based on the number of seats chosen by the user. The problem is when I enter a negative number for one of the seatings, the total is still calculated. Instead, when a negative number is inputted I want an error message to pop up and not calculate the total.
package javatheatreseating;
import java.util.Scanner;
public class JavaTheatreSeating {
public static final double PREMIUM_PRICE = 45.00;
public static final double STANDARD_PRICE = 30.00;
public static final double ECONOMY_PRICE = 21.00;
public static final double TAX_RATE = 0.0825;
public static final double SURCHARGE = 5.00;
public static void main(String[] args) {
int premiumSeats;
int standardSeats;
int economySeats;
double subTotal;
double salesTax;
double surCharge;
double total;
Scanner stdin = new Scanner(System.in);
//INPUT: number of seats sold
System.out.print ("Enter the number of Premium Seats Sold: ");
premiumSeats = stdin.nextInt();
System.out.print ("Enter the number of Standard Seats Sold: ");
standardSeats = stdin.nextInt();
System.out.print ("Enter the number of Economy Seats Sold: ");
economySeats = stdin.nextInt();
//PROCESS: i calculate the total and add the percent of tax based on the seats added
subTotal = premiumSeats * PREMIUM_PRICE + standardSeats * STANDARD_PRICE + economySeats * ECONOMY_PRICE;
salesTax = TAX_RATE * subTotal;
total = subTotal + salesTax + SURCHARGE;
//OUTPUT:
System.out.println();
System.out.println("Subtotal: " + subTotal);
System.out.println("Tax: " + salesTax);
System.out.println("surCharge: " + SURCHARGE);
System.out.println("Total: " + total);
}
}
put a while loop around each variable input and keep looping until the user gets it right. I didn't check if this compiles though.
while (true) {
try {
System.out.print ("Enter the number of Premium Seats Sold: ");
premiumSeats = stdin.nextInt();
if (premiumSeats >= 0){
break;
} else {
System.out.print ("Please Enter a positive integer.\n");
}
}
catch (Exception e){
System.out.print ("Please Enter a number.\n");
}
}

(admit.java) My little program is not passing an integer between methods

I am a bit confused, I have a little program I wrote for a school assignment, the constraints I am required to work with is that no method can contain more than 15 lines of code, and we cannot use any for next loops.
What I cant seem to figure out is why I am able to pass an integer to my rounding method and it even returns the rounded value, but then it jumps back to my if / then statements (which it shouldnt be doing) and does not pass the variable.
It is a very rudimentary program I know...my coding skills are not very good, but any help would be appreciated.
The variables I need passed back are testscore and GPA, those need to go back to the main method so they can be stored to new variables and finally pushed off into the results method.
Im still pretty new to coding and the community and how things work...
import java.util.*;
public class Admit {
public static void main(String[] Args) {
Scanner console = new Scanner(System.in);
Introduction();
double testscore = 0;
double GPA = 0;
int Student = 1;
ACTSATScores(1,0,console);
double StudentOneTestScore = testscore;
GPAInfo(0,console);
double StudentOneGPAScore = GPA;
Student = 2;
ACTSATScores(2,0,console);
double StudentTwoTestScore = testscore;
GPAInfo(0,console);
double StudentTwoGPAScore = GPA;
DisplayResults(StudentOneTestScore,StudentOneGPAScore,StudentTwoTestScore,StudentTwoGPAScore);
}
public static void Introduction() {
System.out.println("This program compares two applicants to");
System.out.println("determine which one seems like the stronger");
System.out.println("applicant. For each candidate I will need");
System.out.println("either SAT or ACT scores plus a weighted GPA.");
System.out.println();
}
public static double ACTSATScores(int Student,double testscore,Scanner console) {
System.out.println("Information for applicant #" + Student + ":");
System.out.print("do you have 1) SAT scores or 2) ACT scores? ");
int ACTSAT = console.nextInt();
if ( ACTSAT == 1) {
SAT(Student,testscore,console);
}
if ( ACTSAT == 2) {
ACT(Student,testscore,console);
}
return testscore;
}
public static double SAT(int Student,double testscore,Scanner console) {
System.out.print("SAT math? ");
int SATMath = console.nextInt();
System.out.print("SAT critical reading? ");
int SATReading = console.nextInt();
System.out.print("SAT writing? ");
int SATWriting = console.nextInt();
testscore = ( ( 2 * SATMath + SATReading + SATWriting ) / 32);
System.out.println("exam score = " + roundNumber(testscore));
return ( ( 2 * SATMath + SATReading + SATWriting ) / 32);
}
public static double ACT(int Student,double testscore,Scanner console) {
System.out.print("ACT English? ");
int ACTEnglish = console.nextInt();
System.out.print("ACT math? ");
int ACTMath = console.nextInt();
System.out.print("ACT reading? ");
int ACTReading = console.nextInt();
System.out.print("ACT science? ");
int ACTScience = console.nextInt();
testscore = ( ( 2 * ACTMath + ACTEnglish + ACTReading + ACTScience ) / 1.8 );
System.out.println("exam score = " + roundNumber(testscore));
return testscore;
}
public static double GPAInfo(double GPA,Scanner console) {
System.out.print("overall GPA? ");
double OverallGPA = console.nextDouble();
System.out.print("max GPA? ");
double MaxGPA = console.nextDouble();
System.out.print("Transcript Multiplier? ");
double TranscriptMultiplier = console.nextDouble();
GPA = ( OverallGPA / MaxGPA * 100 * TranscriptMultiplier );
System.out.println("GPA score = " + roundNumber(GPA));
return GPA;
}
public static double roundNumber(double number) {
return (Math.round(number * 10)) / 10.0;
}
public static double DisplayResults(double StudentOneTestScore, double StudentOneGPAScore, double StudentTwoTestScore, double StudentTwoGPAScore) {
System.out.println("First applicant overall score = " + StudentOneTestScore + StudentOneGPAScore);
System.out.println("Second applicant overall score = " + StudentTwoTestScore + StudentTwoGPAScore);
if ( StudentOneTestScore + StudentOneGPAScore > StudentTwoTestScore + StudentTwoGPAScore ) {
System.out.println("The first applicant seems to be better");
}
else if ( StudentOneTestScore + StudentOneGPAScore < StudentTwoTestScore + StudentTwoGPAScore ) {
System.out.println("The second applicant seems to be better");
}
else {
System.out.println("The two applicants seem to be equal");
}
return StudentOneTestScore;
}
}
Expected output:
This program compares two applicants to
determine which one seems like the stronger
applicant.  For each candidate I will need
either SAT or ACT scores plus a weighted GPA.
 
Information for applicant #1:
    do you have 1) SAT scores or 2) ACT scores? 1
    SAT math? 450
    SAT critical reading? 530
    SAT writing? 490
    exam score = 60.0
    overall GPA? 3.4
    max GPA? 4.0
    Transcript Multiplier? 0.9
    GPA score = 76.5
 
Information for applicant #2:
    do you have 1) SAT scores or 2) ACT scores? 2
    ACT English? 25
    ACT math? 20
    ACT reading? 18
    ACT science? 15
    exam score = 54.4
    overall GPA? 3.3
    max GPA? 4.0
    Transcript Multiplier? 0.95
    GPA score = 78.4
 
First applicant overall score  = 136.5
Second applicant overall score = 132.8
The first applicant seems to be better
My output
This program compares two applicants to
determine which one seems like the stronger
applicant. For each candidate I will need
either SAT or ACT scores plus a weighted GPA.
Information for applicant #1:
do you have 1) SAT scores or 2) ACT scores? 1
SAT math? 450
SAT critical reading? 530
SAT writing? 490
exam score = 60.0
overall GPA? 3.4
max GPA? 4.0
Transcript Multiplier? 0.9
GPA score = 76.5
Information for applicant #2:
do you have 1) SAT scores or 2) ACT scores? 2
ACT English? 25
ACT math? 20
ACT reading? 18
ACT science? 15
exam score = 54.4
overall GPA? 3.3
max GPA? 4.0
Transcript Multiplier? 0.95
GPA score = 78.4
First applicant overall score = 0.00.0
Second applicant overall score = 0.00.0
The two applicants seem to be equal
Corrected Code for anyone that needs it
import java.util.*;
public class Admit {
public static void main(String[] Args) {
Scanner console = new Scanner(System.in);
Introduction();
int Student = 1;
double StudentOneTestScore = ACTSATScores(1,console);
double StudentOneGPAScore = GPAInfo(0,console);
Student = 2;
double StudentTwoTestScore = ACTSATScores(1,console);
double StudentTwoGPAScore = GPAInfo(0,console);
DisplayResults(StudentOneTestScore,StudentOneGPAScore,StudentTwoTestScore,StudentTwoGPAScore);
}
public static void Introduction() {
System.out.println("This program compares two applicants to");
System.out.println("determine which one seems like the stronger");
System.out.println("applicant. For each candidate I will need");
System.out.println("either SAT or ACT scores plus a weighted GPA.");
System.out.println();
}
public static double ACTSATScores(int Student,Scanner console) {
double testscore = 0;
System.out.println("Information for applicant #" + Student + ":");
System.out.print(" do you have 1) SAT scores or 2) ACT scores? ");
int ACTSAT = console.nextInt();
if ( ACTSAT == 1) {
testscore = SAT(console);
}
if ( ACTSAT == 2) {
testscore = ACT(console);
}
return testscore;
}
public static double SAT(Scanner console) {
System.out.print(" SAT math? ");
int SATMath = console.nextInt();
System.out.print(" SAT critical reading? ");
int SATReading = console.nextInt();
System.out.print(" SAT writing? ");
int SATWriting = console.nextInt();
double testscore = ( ( 2 * SATMath + SATReading + SATWriting ) / 32);
System.out.println(" exam score = " + roundNumber(testscore));
return testscore;
}
public static double ACT(Scanner console) {
System.out.print(" ACT English? ");
int ACTEnglish = console.nextInt();
System.out.print(" ACT math? ");
int ACTMath = console.nextInt();
System.out.print(" ACT reading? ");
int ACTReading = console.nextInt();
System.out.print(" ACT science? ");
int ACTScience = console.nextInt();
double testscore = ( ( 2 * ACTMath + ACTEnglish + ACTReading + ACTScience ) / 1.8 );
System.out.println(" exam score = " + roundNumber(testscore));
return testscore;
}
public static double GPAInfo(double GPA,Scanner console) {
System.out.print(" overall GPA? ");
double OverallGPA = console.nextDouble();
System.out.print(" max GPA? ");
double MaxGPA = console.nextDouble();
System.out.print(" Transcript Multiplier? ");
double TranscriptMultiplier = console.nextDouble();
GPA = ( OverallGPA / MaxGPA * 100 * TranscriptMultiplier );
System.out.println(" GPA score = " + roundNumber(GPA));
System.out.println();
return GPA;
}
public static double roundNumber(double number) {
return (Math.round(number * 10)) / 10.0;
}
public static double finalScore(double TestScore, double GPAScore) {
return TestScore + GPAScore;
}
public static double DisplayResults(double StudentOneTestScore, double StudentOneGPAScore, double StudentTwoTestScore, double StudentTwoGPAScore) {
double StudentOneScore = finalScore(StudentOneTestScore,StudentOneGPAScore);
double StudentTwoScore = finalScore(StudentTwoTestScore,StudentTwoGPAScore);
System.out.println("First applicant overall score = " + roundNumber(StudentOneScore)); //StudentOneTestScore + StudentOneGPAScore);
System.out.println("Second applicant overall score = " + roundNumber(StudentTwoScore)); //StudentTwoTestScore + StudentTwoGPAScore);
if ( StudentOneTestScore + StudentOneGPAScore > StudentTwoTestScore + StudentTwoGPAScore ) {
System.out.println("The first applicant seems to be better");
} else if ( StudentOneTestScore + StudentOneGPAScore < StudentTwoTestScore + StudentTwoGPAScore ) {
System.out.println("The second applicant seems to be better");
} else {
System.out.println("The two applicants seem to be equal");
}
return StudentOneTestScore;
}
}
It seems you have a problem when using methods, returning types and inmutable types.
In the main section:
double testscore = 0;
double GPA = 0;
int Student = 1;
ACTSATScores(1,0,console);
double StudentOneTestScore = testscore;
GPAInfo(0,console);
double StudentOneGPAScore = GPA;
Student = 2;
ACTSATScores(2,0,console);
double StudentTwoTestScore = testscore;
You are assigning those variables the value "testscore". But you really never change that value, and the way you wrote the methods makes me believe that you are expecting testscore to change inside the methods.
Try printing the value of testscore in the main to see that it does not change its value (it should be always 0 in that main). This might be what you are looking for:
double StudentOneTestScore = ACTSATScores(1,0,console);

java loop to repeat program

I am extremely new to java I am in my second week in classes or so--
I need my program to keep going or exit according to the user. It is a payroll calculation and I want the end to say "Do you want to continue (y/n)" I want Y to repeat my entire program of questions and no to end program. I am using Jgrasp and I am very very new. I am assuming it needs a loop and I am not totally sure, I just got this to run and compile correctly-- it runs correctly for me so it is a good start and I am hoping to get help on how to do this as I am seeing a ton of different ways and different programs for it. thanks.
import java.util.Scanner;
public class calculations {
public static void main(String [] args) {
Scanner reader = new Scanner(System.in);
Scanner in = new Scanner(System.in);
double Regpay;
double Payperhour;
int HoursAweek;
double Pay;
double OvertimeHours;
double OvertimePay;
double Dependants;
double SocSecTax;
double FederalTax;
double StateTax;
int UnionDues;
double AllTaxes;
double FinalPay;
String playAgain;
System.out.print("Enter your pay per hour:");
Payperhour = reader.nextDouble ();
System.out.print("Enter your regular Hours a week:");
HoursAweek = reader.nextInt();
System.out.print("Enter your overtime hours:");
OvertimeHours = reader.nextDouble();
Regpay = Payperhour * HoursAweek;
OvertimePay = OvertimeHours * 1.5 * Payperhour;
Pay = OvertimePay + Regpay;
SocSecTax = Pay * .06;
FederalTax = Pay * .14;
StateTax = Pay * .05;
UnionDues = 10;
AllTaxes = SocSecTax + FederalTax + StateTax + UnionDues;
FinalPay = Pay -= AllTaxes;
System.out.println("Your pay this week will be " +FinalPay);
{
System.out.println("How many Dependants:");
Dependants = reader.nextInt();
if (Dependants >= 3) {
Dependants = Pay + 35;
System.out.println("Your Pay is:" +Dependants);
} else if(Dependants < 3) {
System.out.println("Your Pay is:" +Pay);
}
}
}
}
Here is the basic idea with your code:
import java.util.Scanner;
public class calculations{
public static void main(String [] args) {
Scanner reader = new Scanner(System.in);
Scanner in = new Scanner(System.in);
double Regpay;
double Payperhour;
int HoursAweek;
double Pay;
double OvertimeHours;
double OvertimePay;
double Dependants;
double SocSecTax;
double FederalTax;
double StateTax;
int UnionDues;
double AllTaxes;
double FinalPay;
String playAgain;
int runAgain = 1;
while (runAgain == 1) {
System.out.print("Enter your pay per hour:");
Payperhour = reader.nextDouble();
System.out.print("Enter your regular Hours a week:");
HoursAweek = reader.nextInt();
System.out.print("Enter your overtime hours:");
OvertimeHours = reader.nextDouble();
Regpay = Payperhour * HoursAweek;
OvertimePay = OvertimeHours * 1.5 * Payperhour;
Pay = OvertimePay + Regpay;
SocSecTax = Pay * .06;
FederalTax = Pay * .14;
StateTax = Pay * .05;
UnionDues = 10;
AllTaxes = SocSecTax + FederalTax + StateTax + UnionDues;
FinalPay = Pay -= AllTaxes;
System.out.println("Your pay this week will be " + FinalPay);
{
System.out.println("How many Dependants:");
Dependants = reader.nextInt();
if (Dependants >= 3) {
Dependants = Pay + 35;
System.out.println("Your Pay is:" + Dependants);
} else if (Dependants < 3) {
System.out.println("Your Pay is:" + Pay);
}
}
System.out.println("Again??? Press 1 to run again and 0 to exit");
runAgain = reader.nextInt();
}
}
}
Here's a guide for you..
You can create a method for the transaction.
//Place this on your main method
do{
//call the method
transaction();
//ask if the user wants to repeat the program
System.out.print("Do you want to continue (y/n)");
input = reader.nextLine();
}while(input.equalsIgnoreCase("Y"))
public void transaction(){
//your transaction code here..
}
Here is a brief idea of how to do this:
String choice = "y";
while(true) {
//Take input
System.out.print("Do you want to continue (y/n)?");
choice = reader.nextLine();
if(choice.equalsIgnoreCase("n")) break;
//else do your work.
}
You can also sue do-while to get what you want. You may need to make some changes but then that is the entire idea. It is just a hint.

Java MPG calculator

I've finished most of the code for a simple MPG calculator. My main issue right now is at the end of the program I need it to calculate the average MPG of all of the miles the user decides to enter in.
I'm getting some number, but it isn't the correct one. If you could beseech your knowledge on me, that would be amazing. Please let me know if there are other problems as well, I'm up to whatever criticism.
import java.util.Scanner;
public class GasMileage {
public static void main(String[] args) {
GasMileage mileage1 = new GasMileage();
GasMileage mileage2 = new GasMileage();
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the MPG calculator!");
double counterM;
double counterG;
double mileage;
double gallons;
double mpg;
double average;
String repeat = "yes";
while (repeat.equals("Yes") ||
repeat.equals("yes") ||
repeat.equals("y") ||
repeat.equals("Y")) {
System.out.println("Enter miles driven: ");
mileage = input.nextDouble();
counterM = mileage++;
mileage1.setMilesDriven(mileage);
mileage2.setMilesDriven(counterM);
System.out.println("Enter gallons used: ");
gallons = input.nextDouble();
counterG = gallons++;
mileage1.setGallonsUsed(gallons);
mileage2.setGallonsUsed(counterG);
mileage1.setMpg(mileage, gallons);
mileage2.setMpg(counterM, counterG);
mpg = mileage1.getMpg();
System.out.println("Your mpg is: " + mpg);
System.out.println("repeat? ");
repeat=input.next();
}
average = mileage2.getMpg();
System.out.println("Your total average mpg is: " + average);
}
double milesDriven;
double gallonsUsed;
double mpg1;
public void setMilesDriven(double Miles) {
milesDriven = milesDriven + Miles;
}
public void setGallonsUsed(double Gallons) {
gallonsUsed = gallonsUsed+Gallons;
}
public double getMilesDriven() {
return milesDriven;
}
public double getGallonsUSed() {
return gallonsUsed;
}
public void setMpg(double setM, double setG) {
mpg1 = (setM) / setG;
}
public double getMpg() {
return mpg1;
}
}
mileage++ will actually increase the value of mileage.
int mileage = 1;
int gallons = mileage++; // mileage will equal 2 after this
As a side note never wait until the end to clean/format your code. Clean code will be easier to read hence easier to spot problems.
I found few problem in the code:
Use repeat.equalsIgnoreCase("YES") instead.
Why mileage++ & gallons++, this will change your input.
setMpg() is not using values which is already present in the Object.
mileage1 is not resetting inside the loop so previous results will be updated instead of creating new result.
If I am not wrong, you want to print the Mileage for each trip and overall mileage. So updated code will be like:
import java.util.Scanner;
public class GasMileage {
public static void main(String[] args) {
GasMileage mileage2 = new GasMileage();
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the MPG calculator!");
double mileage;
double gallons;
double mpg;
double average;
String repeat = "yes";
while (repeat.equalsIgnoreCase("YES")) {
GasMileage mileage1 = new GasMileage();
System.out.println("Enter miles driven: ");
mileage = input.nextDouble();
mileage1.setMilesDriven(mileage);
mileage2.setMilesDriven(mileage);
System.out.println("Enter gallons used: ");
gallons = input.nextDouble();
mileage1.setGallonsUsed(gallons);
mileage2.setGallonsUsed(gallons);
mpg = mileage1.getMpg();
System.out.println("Your mpg is: " + mpg);
System.out.println("repeat? ");
repeat = input.next();
}
average = mileage2.getMpg();
System.out.println("Your total average mpg is: " + average);
}
double milesDriven;
double gallonsUsed;
double mpg1;
public void setMilesDriven(double Miles) {
milesDriven = milesDriven + Miles;
}
public void setGallonsUsed(double Gallons) {
gallonsUsed = gallonsUsed + Gallons;
}
public double getMilesDriven() {
return milesDriven;
}
public double getGallonsUSed() {
return gallonsUsed;
}
public double getMpg() {
return milesDriven / gallonsUsed;
}
}
Output:
Welcome to the MPG calculator!
Enter miles driven: 100
Enter gallons used: 25
Your mpg is: 4.0
repeat? yes
Enter miles driven: 200
Enter gallons used: 20
Your mpg is: 10.0
repeat? n
Your total average mpg is: 6.666666666666667 (300 / 45)
If you want incremental mileage then move back mileage1 outside the loop. So output will be like:
Welcome to the MPG calculator!
Enter miles driven: 100
Enter gallons used: 25
Your mpg is: 4.0
repeat? yes
Enter miles driven: 200
Enter gallons used: 20
Your mpg is: 6.666666666666667
repeat? n
Your total average mpg is: 6.666666666666667 (300 / 45)

return issue for method

I am having an issue with a method returning to the main method. It is saying that amount in "return amount" cannot be resolved to a variable. Where am I off on this??
This is the message I get:
Multiple markers at this line
- Void methods cannot return a
value
- amount cannot be resolved to a
variable
Here is the code:
import java.util.Scanner;
public class Investment {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table
}
public static double futureInvestmentValue(double amount, double interest, int years) {
double monthlyInterest = interest/1200;
double temp;
double count = 1;
while (count < years)
temp = amount * (Math.pow(1 + monthlyInterest,years *12));
amount = temp;
System.out.print((count + 1) + " " + temp);
}
{
return amount;
}
}
You curly braces are not correct. The compiler - and me - was confused about that.
This should work (at least syntactically):
import java.util.Scanner;
public class Investment {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years));
}
public static double futureInvestmentValue(
double amount, double interest, int years) {
double monthlyInterest = interest / 1200;
double temp = 0;
double count = 1;
while (count < years)
temp = amount * (Math.pow(1 + monthlyInterest, years * 12));
amount = temp;
System.out.print((count + 1) + " " + temp);
return amount;
}
}
Remove amount from its own scope As a start. Also from the method futureInvestmentValue, you take in amount as an argument but the value is never modified so you're returning the same value being passed which is most likely not the desired outcome.
remove return amount from its own scope
the method futureInvestmentValue... You can't modify any of the parameters inside the method so you have to declare another variable besides amount inside the method (maybe it's the temp variable you keep using) and return that instead
when you return something, the return statement is always inside the method. Never outside it while inside its own braces (never seen this before...)
import java.util.Scanner;
public class Investment {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table
}
public static double futureInvestmentValue(double amount, double interest, int years) {
double monthlyInterest = interest/1200;
double temp;
double count = 1;
while (count < years) {
temp = amount * (Math.pow(1 + monthlyInterest,years *12));
System.out.print((count + 1) + " " + temp);
}
return amount;
}
}

Categories

Resources