How to store 2 variables and compare them - java

I am trying to accept user input for two people's hourly wage and the amount of hours of overtime they work per year.
using an algorithm I have researched, the program will tell both people the amount of money they make per year and the amount of taxes they pay, which is based on the amount that they make.
This is all fine and dandy. However, what I am now trying to do is to add a line at the end of the program which states who is paying more taxes. This would be accomplished with the method whoPaysMoreTaxes, but I have no idea what to include in that method. I know I would need a simple if/ else if/ else statement to get the job done, but I do not know how I would go about storing the taxes of person 1 and the taxes of person 2 and compare them. The output should be as follows I believe. The numbers 22, 100, 58, and 260 are user input:
Person 1's hourly wage: 22
Person 1's overtime hours for the year: 100
You will make $45540 this year
And you will pay $9108 in taxes
Person 2's hourly wage: 58
Person 2's overtime hours for the year: 260
You will make $133980 this year
And you will pay $40194 in taxes.
Person 2 is paying more taxes.
The issue I am having is finding a way to produce that final line that says who is paying more taxes.
public class conditionalsAndReturn
{
public static void main(String[] args)
{
Scanner console = new Scanner(System.in);
taxes(console, 1);
taxes(console, 2);
}
public static void taxes(Scanner console, int personNum)
{
System.out.print("Person " + personNum + "'s hourly wage: ");
int wage = console.nextInt();
System.out.print("Person " + personNum + "'s overtime hours for the year: ");
double totalOvertimeHours = console.nextInt();
int salary = annualSalary(wage, totalOvertimeHours);
System.out.println("You will make $" + salary + " this year");
System.out.println("And you will pay $" + taxation(salary) + " in taxes");
System.out.println();
}
public static int annualSalary(int wage, double totalOvertimeHours)
{
double workHoursPerWeek = 40 + totalOvertimeHours / 48;
return (int)(weeklyPay(wage, workHoursPerWeek) * 48);
}
public static double weeklyPay(int wage, double workHoursPerWeek)
{
if (workHoursPerWeek > 40)
{
return (wage * 40) + ((wage + wage / 2.0) * (workHoursPerWeek - 40));
}
else
{
return wage * workHoursPerWeek;
}
}
public static int taxation(int salary)
{
if (salary < 20000)
{
return 0;
}
else if (salary > 100000)
{
return salary * 3 / 10;
}
else
{
return salary * 2 / 10;
}
}
public static String whoPaysMoreTaxes(
}

The OOP conform coding would be, to have a class person (or better employee), with the fields: personNum, one or more of the three wage/salary variables, taxation. Add name and such if needed.
Now you can use instances of those class to store the accumulated data, and compare the objects with a compareTo.

If you were to follow true Object Oriented programming principles, then you might create a separate class which represents a Person object (or consider a nested class). Then each Person instance could have the attributes:
hourly_wage
overtime_hours
income
taxes_owed
You would then want to create as many People classes as you need, using the class instances to store data. You could then modify your method header to be:
public Person who_payes_more_taxes(Person p1, Person p2): { ... }
Inside the method you would need to decide how to compare taxes, but most likely it will look something like:
if (p1.taxes_owed > p2.taxes_owed) { return p1 }

You're definitely on the right track. I would use more variables to simplify comparing the taxes:
import java.util.Scanner;
public class ConditionalsAndReturn
{
public static void main(String[] args)
{
int personOneWage;
int personOneOvertime;
double personOnePayBeforeTax;
double personOneTaxes;
double personOneNetIncome;
int personTwoWage;
int personTwoOvertime;
double personTwoPayBeforeTax;
double personTwoTaxes;
double personTwoNetIncome;
Scanner scan = new Scanner(System.in);
System.out.print("Person 1's hourly wage: ");
personOneWage = scan.nextInt();
System.out.print("Person 1's overtime hours for the year: ");
personOneOvertime = scan.nextInt();
personOnePayBeforeTax = (40 * personOneWage) + (personOneOvertime * personOneWage * 1.5);
personOneTaxes = taxes(personOnePayBeforeTax);
personOneNetIncome = personOnePayBeforeTax - personOneTaxes;
System.out.println("You will make $" + personOneNetIncome + " this year");
System.out.println("And you will pay $" + personOneTaxes + " in taxes");
System.out.print("Person 2's hourly wage: ");
personTwoWage = scan.nextInt();
System.out.print("Person 2's overtime hours for the year: ");
personTwoOvertime = scan.nextInt();
personTwoPayBeforeTax = (40 * personTwoWage) + (personTwoOvertime * personTwoWage * 1.5);
personTwoTaxes = taxes(personTwoPayBeforeTax);
personTwoNetIncome = personTwoPayBeforeTax - personTwoTaxes;
System.out.println("You will make $" + personTwoNetIncome + " this year");
System.out.println("And you will pay $" + personTwoTaxes + " in taxes");
if (personOneTaxes > personTwoTaxes)
{
System.out.println("Person 1 is paying more in taxes.");
}
else
{
System.out.println("Person 2 is paying more in taxes.");
}
scan.close();
}
private static double taxes(double payBeforeTax)
{
if (payBeforeTax < 20000)
{
return 0;
}
else if (payBeforeTax > 100000)
{
return payBeforeTax * 3 / 10;
}
else
{
return payBeforeTax * 2 / 10;
}
}
}

Related

Sales receipt, multiple choices (if/else), while loop, customer ability to change selections

None of the other posts seemed to fit accordingly.
I'm really struggling with producing this program without errors. I've come up with this below, however, if you run the program it is not adequately looping and if you choose the same candy twice the math isn't adding to the previous selection.
For the second part, I need to recode to adjust so that the customer may change the current selections if they'd like. So, I need the first part running perfectly.
import java.util.Scanner; //Program uses Scanner
import java.util.Calendar; //Program uses calendar
public class ClairAndreaG005PA2
{//Begin class ClairAndreaG005PA2
public static void main(String[] args)
{
/**Begin method. The customer is asked whether to proceed with a candy purchase.
* As long as the candy choice is in the proper range, prompt for the quantity,
* calculate the item total and subtotal; otherwise, print an error message. If it’s
* the first item in the purchase, ass it to the sales, a receipt with a $ sign for the
* item total. If not format the item without a $ sign for the item total
* and add it to the sales receipt. Start the process again when the customer wants
* to add another candy purchase. When the customer is done, the sales tax and the
* total calculated, the sales receipt is finalized and printed.
*/
int quantity = 0,
formatFirstItem = 1,
choice = 0;
final double TAX_RATE = .0825; //tax rate declared
double price = 0,
itemTotal = 0,
subtotal = 0,
taxAmount = 0,
total = 0;
char proceed = ' '; //proceed variable for loop
String candy = new String();
Calendar dateTime = Calendar.getInstance(); //situational date and time
Scanner input = new Scanner(System.in);
String salesReceipt = String.format("%n%nFAIRYTALE SWEETS%n"
+ "North Star Mall%n"
+ "San Antonio, TX%n"
+ "Date: %n", dateTime
+ "Time: %n", dateTime);
//Initiating variables and calculations
System.out.printf("%nDo you want to proceed with your candy purhase? \'Y\' or \'N\': ");
proceed = input.nextLine().charAt(0);
//End Prompt 1
while(Character.toUpperCase(proceed) == 'Y')
{
System.out.printf("%nFAIRYTALE SWEETS"
+ "%n%n1. Arabian Nights Chocolate Coins - 1 lb. Bag %5s%,7.2f"
+"%n2. Beauty and the Beast Lollipops - 1 lb. Bag %,12.2f"
+"%n3. Mad Hatter Jelly Beans - 1 lb. Bag %,20.2f"
+"%n4. Pinocchio's Candy Cones - Each %,23.2f"
+"%n5. Sleeping Beauty Caramel Apples - Each %,17.2f"
+"%n%nEnter your choice: ", "$", 2.25, 2.50, 1.75, 0.75, 1.25);
choice = input.nextInt();
//Prompt 2 Candy Menu
if(choice > 0)
{if(choice < 6)
{if(choice == 1)
{candy = "Arabian Nights Chocolate Coins";
price = 2.25;
}
else
{if(choice == 2)
{ candy = "Beauty and the Beast Lollipops";
price = 2.50;
}
else
{if(choice == 3)
{candy = "Mad Hatter Jelly Beans";
price = 1.75;
}
else
{if(choice == 4 )
{candy = "Pinocchio's Candy Cones";
price = 0.75;
}
else
{candy = "Sleeping Beauty Caramel Apples";
price = 0.75;
}
}
}
}
System.out.printf("%nQuantity for %s: ", candy);
quantity = input.nextInt();
//quantity of selections
}
else
{ System.out.println("Invalid candy choice! Try again.");
}
//Choices and selections
itemTotal = quantity * price; //calculation 1
subtotal += itemTotal; //calculation 2
System.out.printf("%nWould you like to make another candy purchase? \'Y\' or \'N\': ");
proceed = input.next().charAt(0);
if(formatFirstItem == 1 )
{
salesReceipt += String.format("%n%s"
+ "%n %d # $%.2f ex. %-24s $%,10.2f%n", candy,
quantity, price, " ", itemTotal);
formatFirstItem = 0;
}
else
{
salesReceipt += String.format("%s"
+ "%n %d # $%.2f ea. %-25s %,10.2f%n", candy,
quantity, price, " ", itemTotal);
}
//End if FormatFIrst Item is 1 or else formatFirstItem NOT 1
}
}//End while loop selection
taxAmount = TAX_RATE * subtotal;// calculation 3
total = taxAmount + subtotal; // calculation 4
salesReceipt += String.format("%n%36s %-6s $%,10.2f"
+ "%n%36s %-7s %,10.2f"
+ "%n%n%36s %-6s $%,10.2f%n", "SUBTOTAL: ", " ",
subtotal, "TAX # 8.250%: ", " ", taxAmount,
"TOTAL: ", " ", total);
System.out.printf("%s", salesReceipt);
}
}
Let me know!

Java For Loop not ending with Sentinel

I am creating a program that takes in employee payroll information and then displays the average and total afterwards. Everything seems to be working correctly except for the fact I cannot get the program to end when I enter the sentinel value of -1 for the hours worked so that the program can display the total and average of the employees. Any help is greatly appreciated.
import java.util.Scanner;
public class PayrollDo
{
public static void main(String[] args)
{
int hoursWorked = 0;
int grossPay = 0;
int empCounter = 0;
int total = 0;
Scanner keyboard = new Scanner(System.in);
do {
total = total + grossPay; // add gross to total
empCounter = empCounter + 1; // incriment counter
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextInt();
System.out.print("Enter hourly wage: ");
int hourlyWage = keyboard.nextInt();
//System.out.println("Grosspay is " + (hourlyWage * hoursWorked));
keyboard.nextLine();
System.out.println("Enter employee name ");
String name = keyboard.nextLine();
} while(hoursWorked != -1);
// if user entered at least one employee
if (empCounter != 0) {
// use number with decimal point to calculate average of employees
int average = (int) total / empCounter;
// display total and average (with two digits of precision)
System.out.printf("%nTotal of the %d employees entered is %d%n",
empCounter, total);
System.out.printf("Employee average is " + average);
}
else {
// no employees were entered, so output appropriate message
System.out.println("No employees were entered");
}
}
}
Test soon after entering the value whether it is -1 or not
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextInt();
if (hoursWorked == -1) break;
edit
I think you will also have trouble with Integer division http://stackoverflow.com/questions/4685450/why-is-the-result-of-1-3-0
You could restructure your do loop to put the hoursWorked input last (otherwise you have to complete the entries) - and also, add a conditional to reject all entries that cycle, otherwise empCounter is off by one. And I think the total calculation needs reworking too:
do {
System.out.println("Enter employee name ");
String name = keyboard.nextLine();
System.out.print("Enter hourly wage: ");
int hourlyWage = keyboard.nextInt();
System.out.print("Enter hours worked: ");
hoursWorked = keyboard.nextInt();
keyboard.nextLine();
if (hoursWorked != -1) {
total = total + (hourlyWage * hoursWorked); //
empCounter = empCounter + 1; // incriment counter
}
} while(hoursWorked != -1);
In cases like this it can be simpler to have a question like "More Employees? (y/n)" that is used to terminate the loop, using a boolean flag as the while terminator.

(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);

NetBeans : Trying to create a class file to work with another program

Having trouble compiling these two files to run together.
I get a "can't find or load main class" error or a "erroneous tree" error.
Never asked for help on here before, hope this works :)
package savingsaccount;
import java.util.Scanner;
public class SavingsAccount
{
public static void main(String[] args)
{
double begginingBalance, deposit, withdraw;
int months;
double monthlyRate;
double plus = 0.0;
double minus = 0.0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the balance at beggining of " +
"accounting period.");
begginingBalance = keyboard.nextDouble();
System.out.println("Please enter number of months in current " +
"accounting period.");
months = keyboard.nextInt();
System.out.println("Enter the annual interest rate.");
monthlyRate = keyboard.nextDouble();
a7main accounting = new a7main();
for(int month = 1; month<=months; month++)
{
System.out.println("Enter the amount of deposits for month " +
month + " : ");
plus = keyboard.nextDouble();
accounting.deposits(plus);
System.out.println("Enter the amount of withdrawals for" +
" month " + month + ": ");
minus = keyboard.nextDouble();
accounting.withdrawals(minus);
accounting.interest(monthlyRate);
}
System.out.println("The account balance is: " +
accounting.getBalance());
System.out.println("The total amount of deposits is:" + plus);
System.out.println("The total amount of withdrwals is: " + minus);
System.out.println("The earned interest is: " +
accounting.getRate());
}
}
HERE IS THE CLASS FILE
I am trying to use the methods in this file to calculate and hold the values from the other file.
public class a7main
{
private double totalBalance;
private double interestRate;
public a7main(double balance,double rate)
{
totalBalance = balance;
interestRate = rate;
}
public void deposits(double deposit)
{
totalBalance = totalBalance+deposit;
}
public void withdrawals(double withdraw)
{
totalBalance = totalBalance-withdraw;
}
public void interest(double rate)
{
interestRate = totalBalance*rate;
}
public double getBalance()
{
return totalBalance;
}
public double getRate()
{
return interestRate;
}
}
#Alex Goad -
Add package name in a7main class
You have created parameterized constructor and no default constructor.
a7main accounting = new a7main();
The above line will look for default constructor like
public a7main(){
}

Proper Formatting

I have the program working I just need help cutting off the extra numbers, Im not very skilled at using the printf statements when printing in Java. When I run it I get output like 1225.043 Here is what I have:
import java.util.Scanner;
public class Comparison {
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
float amount;
double principal = 1000.00;
double rate;
System.out.println("Enter interest rate");
rate = keyboard.nextDouble();
System.out.println("Year" +" "+ "Amount on deposit");
for(int year = 1; year <= 10; ++year)
{
amount = (float) (principal * Math.pow(1.0 + rate, year));
System.out.println(year+ " "+ amount);
System.out.println();
}
}
}
Try
System.out.printf("%2d %.2f%n", year, amount);
Output:
Enter interest rate
0.1
Year Amount on deposit
1 1100.00
2 1210.00
3 1331.00
4 1464.10
5 1610.51
6 1771.56
7 1948.72
8 2143.59
9 2357.95
10 2593.74

Categories

Resources