I am writing a compound interest program in java but i am still pretty new at th
ID: 3545450 • Letter: I
Question
I am writing a compound interest program in java but i am still pretty new at this and i can't seem to get this to run because it is asking for a main but i have one already. this is what i have. i need to know how to fix the main and if i run it will it do what i need. plus i need a toString method
import java.util.Scanner;
class Investment2 {
float amount;
float rate;
int years;
void Investment() {
amount=0;
rate=0;
years=0;
}
void setAmount (float a) {
amount=a;
}
void setRate (float r) {
rate=r;
}
void setYears (int y) {
years=y;
}
void main(String[] Investment2, double CompoundInterest) {
Scanner input = new Scanner(System.in);
float a = 0;
float r = 0;
float Y = 0;
float compoundInterest = 0;
System.out.print("Enter the Principal amount : ");
float Amount = input.nextFloat();
System.out.print("Enter the Rate : ");
rate = input.nextFloat();
System.out.print("Enter the Time : ");
float Years = input.nextFloat();
CompoundInterest = Amount * Math.pow((1 + rate/100),Years);
System.out.println("");
System.out.println("The Compound Interest is : "
+ compoundInterest);
}
}
Explanation / Answer
/* You dont need such complications as constructer and global variable. Every console project in java needs a static void main function with a string array as argument. (public static void main(String[] args) ). That is why ur code was failing mainly. You can change the main declaration and watever u had as argument of main (String[] Investment2, double CompoundInterest), u can now define them locally and use. To make things simple I have posted a simple working code below. You are most welcome to try it out. Ask me if u have any further query or need more */
import java.util.Scanner;
class Investment2 {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the Principal amount : ");
float Amount = input.nextFloat();
System.out.print("Enter the Rate : ");
double rate = input.nextFloat();
System.out.print("Enter the Time : ");
float Years = input.nextFloat();
double CompoundInterest = Amount * Math.pow((1 + rate/100),Years);
System.out.println("");
System.out.println("The Compound Interest is : "+ CompoundInterest);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.