I am trying to create a java program that take in a monetary amount $XX.xx and outputs the amount in change e-g number of quarters, dimes, nickels etc.
I am having trouble passing the scanner from the public class to the methods used in converting and then calling said methods in the main method.
Here's my code so far.
import java.util.Scanner;
public class moneyConverter{
double currentAmount = (int)(money *100.00); //variables
public static Scanner scan = new Scanner(System.in);
public static double money = scan.nextDouble();
public static void main(String[] args)
{
double money = 0.0;
money.Dollar();
money.Quarter();
money.Dime();
money.Nickel();
money.Penny();
}
public static void Dollar(Scanner scan){
double dollarAmount = scan.nextDouble() / 100.00;
double currentAmount = dollarAmount % 100.00;
System.out.println("Dollars: " +dollarAmount);
return;
}
public static void Quarter(Scanner scan){
double dollarAmount = scan.nextDouble() / 25.00;
double currentAmount = dollarAmount % 25.00;
System.out.println("Quarters: " +dollarAmount);
return;
}
public static void Dime(Scanner scan){
double dollarAmount = scan.nextDouble() / 10.00;
double currentAmount = dollarAmount % 10.00;
System.out.println("Dimes: " +dollarAmount);
return;
}
public static void Nickel(Scanner scan){
double dollarAmount = scan.nextDouble() / 5.00;
double currentAmount = dollarAmount % 5.00;
System.out.println("Nickels: " +dollarAmount);
return;
}
public static void Penny(Scanner scan){
double currentAmount = currentAmount;
System.out.println("Pennies: " +currentAmount);
return;
}
}
Can anyone help ?
money is a double, which doesn't have the methods you are trying to call on it.
Just call the methods within the moneyConverter class by doing:
Dollar(scan);
Quarter(scan);
Dime(scan);
Nickel(scan);
Penny(scan);
Related
I can't figure out how to return the value of finalCost to the main method and have it printed.
import java.util.*;
public class PhonePlan {
private static double basecost = 8.5;
private static double rate = 0.35;
private static double discount = 0.15;
public static void main(String[] args) {
//Scan input for downloadLimit
Scanner scan = new Scanner(System.in);
System.out.print("Enter the download limit in GB: ");
int downloadLimit = scan.nextInt();
// Call other method
calcMonthlyCharge(downloadLimit);
System.out.println("Plan monthly cost: " + calcMonthlyCharge(finalCost));
}
public static double calcMonthlyCharge(double downloadLimit) {
// Calculate final cost
double fullCost = downloadLimit * rate + basecost;
double planDiscount = fullCost * discount;
double finalCost = fullCost - planDiscount;
return finalCost;
}
}
Specifically, I can't find how to use the returned value in the "println" line.
System.out.println("Plan monthly cost: " + calcMonthlyCharge(finalCost) );
}
public static double calcMonthlyCharge(double downloadLimit) {
// Calculate final cost
double fullCost = downloadLimit * rate + basecost;
double planDiscount = fullCost * discount;
double finalCost = fullCost - planDiscount;
return finalCost;
}
You call calcMonthlyCharge(downloadLimit),but don't store the returnvalue.
When you call System.out.println("Plan monthly cost: " + calcMonthlyCharge(finalCost) );
It is unknown what finalcost is, this is a variable which only exist in the scope of calcMonthlyCharge
Either store the returnvalue of calcMonthlyCharge(downloadLimit) and reuse the value to print, or use calcMonthlyCharge(downloadLimit) in your println with downloadLimit as parameter to get a new returnvalue.
In this HackerRank challenge, I need to find the total meal cost by adding tip_percent which is 20% of the meal_cost and tax_percent which is 8% of the meal_cost and the meal_cost being $12. So the output must be a round number of 15 but my output comes out as $14.
It does seem to work properly with custom values like $12.50 for meal_cost which later totaled comes out as a rounded value of $16. What am I doing wrong here?
static double findMealTotal(double meal_cost, int tip_percent, int tax_percent) {
tip_percent = (int)(meal_cost * tip_percent)/100;
tax_percent = (int)(meal_cost * tax_percent)/100;
return meal_cost + tip_percent + tax_percent;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
double meal_cost = scanner.nextDouble();
int tip_percent = scanner.nextInt();
int tax_percent = scanner.nextInt();
//changed solve to mealTotal
double mealTotal = findMealTotal(meal_cost, tip_percent, tax_percent);
System.out.println(Math.round(mealTotal));
scanner.close();
}
You are using integers. Integers are rounded, so you loose precision over the next calculation. Try using doubles and cast to int at the end.
static void Main(string[] args)
{
double cost = findMealTotal(12, 20, 8);
Console.WriteLine(cost.ToString());
}
static double findMealTotal(double meal_cost, int tip_percent, int tax_percent)
{
double tip = meal_cost * tip_percent / 100;
double tax = meal_cost * tax_percent / 100;
return meal_cost + tip + tax;
}
And don't reuse parameters inside your function. It is bad practice.
I'm a beginner at Java and I'm having trouble understanding why my "Inflate" and "getVolume" methods aren't working. I'm sure they're just simple problems but I'd still like some help so I can fix my code and improve!
import java.util.Scanner;
public class Balloon
{
public static void main(String[] args)
{
Scanner multiplier = new Scanner(System.in);
System.out.println("How much should the radius be increased by? ");
double amount = multiplier.nextDouble();
double radius = 0;
public void Inflate(double amount);
{
double newRadius = radius + amount;
}
public double getVolume();
{
double sVolume = (4/3)*Math.PI*(newRadius*newRadius*newRadius);
System.out.print(sVolume);
}
}
}
I suppose that Ballon is an object you can inflate and has an state of radius, you can moreover get the volume.
The main method here is only to test if balloon works correctly
public class Balloon {
private double radius = 0;
public static void main(String[] args) {
Scanner multiplier = new Scanner(System.in);
System.out.println("How much should the radius be increased by? ");
Balloon balloon=new Balloon();
double amount = multiplier.nextDouble();
balloon.inflate(amount);
double volume = balloon.getVolume();
System.out.print(volume);
}
public void inflate(double amount) {
radius = radius + amount;
}
public double getVolume() {
double sVolume = (4 / 3) * Math.PI * (Math.pow(radius, 3));
return sVolume;
}
}
I'm in my intro to Java class and working on loops this week. I think I have the loop built but my variable within my main CommissionNotifications is undefined.
I think I have to create an object and reference the variable stored in my other class...am I on the right track?
The program asks for annual sales and then calculates there commission payment based upon the bracket they fall into. The commission payment is done through a If statement on the class and then the program displays what they could earn if they increased there sales by 5,000 up to 1.5 * of there sales. IE if they earn 100000 in sales the table should display there initial commission and then what they could earn if they increased there sales to 150000(1.5*)
Here is my class:
public class Calculations {
double TotalSales;
double ComRate = 0.025;
double AnnualSal = 80000;
double compensation;
double SalesTarget;
double Acceleration = 1.5;
double chart;
double ComAccFactor;
public double getCommissionNotifications() {
return CommissionNotifications;
}
public void setCommissionNotifications(double commissionNotifications) {
CommissionNotifications = commissionNotifications;
}
public double CommissionNotifications; {
if (TotalSales > 120000){
CommissionNotifications = AnnualSal + (TotalSales * (ComRate + Acceleration));
} else if (TotalSales > SalesTarget * .8) {
CommissionNotifications = AnnualSal + (TotalSales * ComRate);
} else {;
CommissionNotifications = AnnualSal;
}
}
}
Here is my Main
import java.util.*;
import java.text.*;
public class Paycheck {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);
NumberFormat nf = NumberFormat.getCurrencyInstance();
System.out.println("Enter Total Commission Sales: ");
double TotalSales = input.nextDouble();
double Base = TotalSales;
double finish = TotalSales * 1.5;
System.out.println("Your Total compensation with your annual sales is: " + getCommissionNotifications);
int i = Base + 5000;
while (i <= finish) {
System.out.println(Base);
TotalSales += 5000;
}
}
}
getCommisionNotifications is a member of class Calculations. To access it you will need to create a new Calculations object:
Calculations c = new Calculations();
c.getCommisionNotifications();
This code looks a lot like C# with the capitalized variable names. Anyways, your
getCommissionNotifications method should handle the logic of what to return based on TotalSales. It's also not clear why you need a setter method, so I've commented that out.
As for using the below class, you need an instance of the Calculations class
Calculations calc = new Calculations();
double TotalSales = input.nextDouble();
calc.TotalSales = TotalSales;
// double Base = TotalSales; // Duplicate variable not needed
double finish = TotalSales * 1.5;
System.out.println("Your Total compensation with your annual sales is: " + calc.getCommissionNotifications());
public class Calculations {
double TotalSales;
double ComRate = 0.025;
double AnnualSal = 80000;
double compensation;
double SalesTarget;
double Acceleration = 1.5;
double chart;
double ComAccFactor;
public double getCommissionNotifications() {
if (TotalSales > 120000){
return AnnualSal + (TotalSales * (ComRate + Acceleration));
} else if (TotalSales > SalesTarget * .8) {
return AnnualSal + (TotalSales * ComRate);
} else {
return AnnualSal;
}
}
// Not sure why this is needed... You have a dynamic getter method
//public void setCommissionNotifications(double commissionNotifications) {
// CommissionNotifications = commissionNotifications;
//}
}
I need some help with a program for my programming class. It's a recursive program that takes a subtotal and a gratuity rate given by the user that outputs the full total and the gratuity cost. This is what I've got so far, and for some reason it just doesn't work:
import java.io.*;
import java.until.Scanner;
public class gratuity {
private double total;
private double subTotal;
private double gratRate;
private double newSubTotal;
private double newGratRate;
public static void main(String[] args) {
System.out.print("Enter the subtotal: ");
System.out.print("Enter the gratuity rate: ");
Scanner scan = new Scanner(System.in);
Scanner myScan = new Scanner(System.in);
double subtotal = scan.nextDouble();
double gratRate = myScan.nextDouble();
System.out.println("The Gratuity is: " + newSubtotal);
System.out.println("The Total is: " + Total);
}
public static double computeGratRate() {
double newGratRate = (gratRate/100);
return newGratRAte;
}
public static double computeNewSub() {
double newSubTotal - (subTotal * newGratRate);
return newSubTotal;
}
public static double computeTotal() {
double total = (newSubTotal + newGratRate);
return total;
}
}
If anyone would help me figure out how to fix it, I would be very grateful! Thank you!
A few things.
You are creating new variables called "subtotal" and "gratRate" in Main. These values override the member variables of the class.
Your problem won't compile anyway, because...
All your methods are static, which is OK, but these static methods are accessing non-static variables. Make all your member variables of this class static. (Or make everything outside of Main not static and then have "Main" be a stub to just create an instance of the gratuity class.
You need to import java.util.Scanner, not java.until.Scanner.
This line is a compiler error:
double newSubTotal - (subTotal * newGratRate);
I think you mean:
double newSubTotal = (subTotal * newGratRate);
That should be enough hints for now.... keep trying.
Did you mean:
import java.util.Scanner;
public class Gratuity {
private double subTotal;
private double gratRate;
public static void main(String[] args) {
Gratuity gratuity = new Gratuity();
System.out.print("Enter the subtotal: ");
Scanner scan = new Scanner(System.in);
gratuity.setSubTotal(scan.nextDouble());
System.out.print("Enter the gratuity rate: ");
Scanner myScan = new Scanner(System.in);
gratuity.setGratRate(myScan.nextDouble());
System.out.println("The new GratRate is: " + gratuity.getNewGratRate());
System.out.println("The New Sub is: " + gratuity.getNewSub());
System.out.println("The Total is: " + gratuity.getTotal());
}
public double getNewGratRate() {
return gratRate/100;
}
public double getNewSub() {
return getNewGratRate() * subTotal;
}
public double getTotal() {
return getNewSub() + getNewGratRate();
}
public double getSubTotal() {
return subTotal;
}
public void setSubTotal(double subTotal) {
this.subTotal = subTotal;
}
public double getGratRate() {
return gratRate;
}
public void setGratRate(double gratRate) {
this.gratRate = gratRate;
}
}