public static void main(String args [])
{
double dimes;
double quaters;
Scanner input = new Scanner(System.in);
System.out.print("Enter number of Dimes:");
dimes = input.nextDouble();
System.out.print("Enter Number of Quaters:");
quaters = input.nextDouble();
double dollars= dollar_amount(dimes,quaters);
System.out.println("Dollar Amount total: $" + dollars);
public static double dollar_amount(dimes,quaters);
dollars= dollar_amount(dimes,quaters);
System.out.println("Total dollar amount: $" + dollars);
}
double dollar_amount(double dimes, double quaters);
{
dollars = (0.10 * dimes)+(0.25 * quaters);
}
return dollars;
}
}
}
I have a question on how to call a method. I had follow the hints that I was given, but somehow I don't quite get on calling a method.
hint:
input dimes
input quarters
call the method dollar_amout(dimes, quarters)
dollars = dollar_amount(dimes, quarters)
end main
double dollar_amount(dimes, quarters)
dollars = 0.10 x dimes + 0.25 x quarters
return dollars
end method
I think your method body is wrong
Double dollar_amount(double dimes, double quaters);
{
Double dollars = (0.10 * dimes)+(0.25 * quaters);
return dollars;
}
And your calling it right place. No problem with that
What you are doing is trying to set the value of a variable using a method. You must define the method dollar_amount(dimes,quaters)properly before you can call it.
Paste this code over your current code, into your main class.
public static void main(String args []){
double dimes;
double quaters;
Scanner input = new Scanner(System.in);
System.out.print("Enter number of Dimes:");
dimes = input.nextDouble();
System.out.print("Enter Number of Quaters:");
quaters = input.nextDouble();
double dollars= dollar_amount(dimes,quaters);
System.out.println("Dollar Amount total: $" + dollars);
}
public static double dollar_amount(dimes,quaters){
dollars = (0.10 * dimes)+(0.25 * quaters);
return dollars;
}
You have to make some changes, try like this.
public static void main(String args[]) {
double dimes;
double quaters;
Scanner input = new Scanner(System.in);
System.out.print("Enter number of Dimes:");
dimes = input.nextDouble();
System.out.print("Enter Number of Quaters:");
quaters = input.nextDouble();
double dollars = dollar_amount(dimes, quaters);
System.out.println("Dollar Amount total: $" + dollars);
dollars = dollar_amount(dimes, quaters);
System.out.println("Total dollar amount: $" + dollars);
}
public static double dollar_amount(double dimes, double quaters) {
double dollars = (0.10 * dimes) + (0.25 * quaters);
return dollars;
}
Related
This is my first class and first time using Java. I also need to display 'Employee Name, Rate of Pay, Hours Worked, Overtime Worked, Gross Pay, Total amount of deductions, & Net Pay in the program as well.
package calculatepayprogram;
import java.util.Scanner;
/**
* Calculate Pay Program
* CPT 307: Data Structures & Algorithms
* 7/5/2022
*/
public class CalculatePayProgram{
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
double RateOfPay;
int HoursWorked;
double GrossPay;
double OvertimeHours;
double OvertimePay;
double NetPay;
double TotalDeductions;
String name;
System.out.println("Enter Your Name: ");
name = reader.nextLine();
System.out.print("Enter Your Pay Per Hour: ");
RateOfPay = reader.nextDouble();
System.out.print("Enter Your Hours Worked: ");
HoursWorked = reader.nextInt();
System.out.print("Enter Your Overtime Hours Worked: ");
OvertimeHours = reader.nextDouble();
GrossPay = RateOfPay * HoursWorked;
OvertimePay = OvertimeHours * 1.5 * RateOfPay;
double Pay = OvertimePay + GrossPay;
System.out.println("Your Gross Pay For This Week Will Be $" + Pay);
double FederalTax = .15 * Pay;
double StateTax = .0307 * Pay;
double Medicare = .0145 * Pay;
double SocialSecurity = .062 * Pay;
double Unemployment = .0007 * Pay;
double TotalDeduct = FederalTax + StateTax + Medicare + SocialSecurity + Unemployment;
System.out.println("Your Total Deductions For This Week Will Be $" + TotalDeduct);
NetPay = Pay - TotalDeduct;
System.out.println("Your Net Pay For This Week Will Be $" + NetPay);
}
}
I'm a junior in high school. Having a tough time figuring out how to use money format. I'm doing an exercise in A Guide to Programming in Java (Second Edition) where I have to prompt the employees for the number of burgers, fries, and sodas.
Fries are $1.09, burgers are $1.69, and sodas are $0.99.
Here is my code:
import java.util.Scanner;
/**
* Order pg. 101
*
* Garret Mantz
* 2/10/2016
*/
public class Order {
public static void main(String[]args) {
final double pburgers=1.69;
final double pfries=1.09;
final double psodas=0.99;
final double ptax=0.065;
double burgers;
double fries;
double sodas;
double totaltax;
double total;
double tax;
double tendered;
double change;
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount of burgers: ");
burgers = input.nextDouble();
System.out.print("Enter the amount of fries: ");
fries = input.nextDouble();
System.out.print("Enter the amount of sodas: ");
sodas = input.nextDouble();
System.out.print("Enter the amount tendered: ");
tendered = input.nextDouble();
totaltax = (burgers*pburgers)+(fries*pfries)+(sodas*psodas);
tax = totaltax*ptax;
total = totaltax + tax;
change = tendered - total;
System.out.println("Your total before tax is: \n" + totaltax);
System.out.println("Tax: \n" + tax);
System.out.println("Your final total is: \n" + total);
System.out.println("Your change is: \n" + change);
}
}
I just want to use the money format, but I'm not sure how. I'm sure it's a dumb question, but thank you for helping out!
Change your println to these, and see if that helps:
System.out.format("Your total before tax is: $%-5.2f\n", totaltax);
System.out.format("Tax: $%-5.2f\n", tax);
System.out.format("Your final total is: $%-5.2f\n", total);
System.out.format("Your change is: $%-5.2f\n", change);
There is also this:
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String totalTaxString = formatter.format(totaltax);
String taxString = formatter.format(tax);
String totalString = formatter.format(total);
String changeString = formatter.format(change);
System.out.format("Your total before tax is: %s\n", totalTaxString);
System.out.format("Tax: %s\n", taxString);
System.out.format("Your final total is: %s\n", totalString);
System.out.format("Your change is: %s\n", changeString);
Output:
Your total before tax is: $8.53
Tax: $0.55
Your final total is: $9.08
Your change is: $10.92
I have created a simple tax calculator that will calculate Federal and Provincial taxes for incomes less than or equal to $41,536 and this worked fine.
Now to create a method that will calculate and print the tax, this isn't working for me
Here is the code..
package lab4;
import java.util.Scanner;
/**
*
* #author demo
*/
public class Lab4 {
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
// Q4. Tax Calculation
Scanner sc= new Scanner(System.in);
System.out.print("Enter your income: ");
String userIncome=sc.nextLine();
calculateAndPrintTax();
}
static double calculateAndPrintTax(double userIncome)
{
double uIncome= Double.parseDouble(userIncome);
double federalExemption= 11327.0;
double provincialExemption= 9863.0;
double federalTax = (uIncome- federalExemption) * 0.15;
double provincialTax= (uIncome - provincialExemption) * 0.0505;
double totalTax= federalTax + provincialTax;
System.out.println("Your payable Federal tax is: " + federalTax);
System.out.println("Your payable Provincial tax is: "+ provincialTax);
System.out.println("Total payable tax is: "+ totalTax);
}
}
I changed some stuff and was able to compile and run the program successfully.
public static void main(String[] args)
{
// Q4. Tax Calculation
Scanner sc= new Scanner(System.in);
System.out.print("Enter your income: ");
double userIncome=sc.nextDouble();
calculateAndPrintTax(userIncome);
}
static void calculateAndPrintTax(double userIncome)
{
double federalExemption= 11327.0;
double provincialExemption= 9863.0;
double federalTax = (userIncome- federalExemption) * 0.15;
double provincialTax= (userIncome - provincialExemption) * 0.0505;
double totalTax= federalTax + provincialTax;
System.out.println("Your payable Federal tax is: " + federalTax);
System.out.println("Your payable Provincial tax is: "+ provincialTax);
System.out.println("Total payable tax is: "+ totalTax);
}
}
When you call the calculateAndPrintTax method, you have to enter parameters too, because your method asks for them. So instead of calculateAndPrintTax(); you should have calculateAndPrintTax(userIncome); because that's what the method uses to do all of its calculations. I also changed userIncome to a double to simplify the process.
You must also make calculateAndPrintTax void, and remove the double, because instead of returning any values, it prints them out instead.
Hope I could help.
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
System.out.print("Enter your income: ");
String userIncome=sc.nextLine();
calculateAndPrintTax(userIncome);
}
static void calculateAndPrintTax(String userIncome)
{
double uIncome= Double.parseDouble(userIncome);
double federalExemption= 11327.0;
double provincialExemption= 9863.0;
double federalTax = (uIncome- federalExemption) * 0.15;
double provincialTax= (uIncome - provincialExemption) * 0.0505;
double totalTax= federalTax + provincialTax;
System.out.println("Your payable Federal tax is: " + federalTax);
System.out.println("Your payable Provincial tax is: "+ provincialTax);
System.out.println("Total payable tax is: "+ totalTax);
}
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)
I should write a program that reads balance and interest rate, and displays the value of the account in ten years with anually, monthly and daily compounds.
I have written for yearly compounding and for monthly. In the second loop for monthly rate, program reads value of "balance" after compounding yearly, while I need it read primary value. How is it possible to separate two loops, so they do not influence each other? Here is my code:
import java.util.Scanner;
public class BankInterest {
public static void main(String []args) {
System.out.println("Please enter your balance: ");
Scanner keyboard = new Scanner(System.in);
double balance = keyboard.nextDouble();
int years = 0;
int months = 0;
int days = 0;
System.out.println("Please enter the ann1ual interest rate in decimal form: ");
double interestRate = keyboard.nextDouble();
while (years<10) {
double interest = balance * interestRate;
balance = balance + interest;
years++;
}
System.out.println("Balance after 10 years with annual interest is " + balance);
while (months<120) {
double interest = balance * interestRate/12;
balance = balance + interest;
months++;
}
System.out.println("Balance after 10 years with monthly interest rate is " + balance);
}
}
When program is run and I input 100 for balance and 0.02 to interest rate, yearly compounding works well and displays:
Balance after 10 years with annual interest is 121.89944199947573
And second loop takes this value as balance and displays:
Balance after 10 years with monthly interest rate is 148.86352955791543
While, if my code was right it should display this number: 122.119943386
You can place both loops in different functions and pass both balance and interesRate as arguments, like below.
import java.util.Scanner;
public class BankInterest {
static public double annualInterest(double balance, double interestRate) {
int years = 0;
while (years < 10) {
double interest = balance * interestRate;
balance = balance + interest;
years++;
}
return balance;
}
static public double monthlyInterest(double balance, double interestRate) {
int months = 0;
while (months < 120) {
double interest = balance * interestRate/12;
balance = balance + interest;
months++;
}
return balance;
}
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter your balance: ");
double balance = keyboard.nextDouble();
System.out.println("Please enter the ann1ual interest rate in decimal form: ");
double interestRate = keyboard.nextDouble();
System.out.println("Balance after 10 years with annual interest is " +
annualInterest(balance, interestRate));
System.out.println("Balance after 10 years with monthly interest rate is " +
monthlyInterest(balance, interestRate));
}
}
You need to declare two different variables for initial balance and updated balance so that you can access the original balance in both loops. I have shown this below.
public static void main(String []args) {
System.out.println("Please enter your balance: ");
Scanner keyboard = new Scanner(System.in);
double startingBalance = keyboard.nextDouble();
double finalBalance=0;
int years = 0;
int months = 0;
int days = 0;
System.out.println("Please enter the ann1ual interest rate in decimal form: ");
double interestRateYearly = keyboard.nextDouble();
double interstRateMonthly = interestRateYearly;
double interstRateDayliy = interestRateYearly;
while (years<10) {
double interest = startingBalance * interestRateYearly ;
finalBalance = startingBalance + interest;
years++;
}
System.out.println("Balance after 10 years with annual interest is " + finalBalance);
while (months<120) {
double interest = startingBalance * interstRateMonthly /12;
finalBalance = startingBalance + interest;
months++;
}
System.out.println("Balance after 10 years with monthly interest rate is " + finalBalance);
}
}