I must convert inches into miles. Then calculate the remaining feet after I calculate the miles. Then calculate the remaining inches after that. The output I'm getting is 19 miles, 19 feet, 7 inches when it should be 19 miles, 2560 feet, 7 inches.
import java.util.Scanner;
/**
* #author abc
* #version 1-28-2014
*/
public class DistanceConverter {
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in);
int numOfInches;
System.out.print("Enter the raw measurement in inches: ");
numOfInches = userInput.nextInt();
if (numOfInches < 0) {
System.out.println("Measurement must be non-negative!");
}
int feet = numOfInches / 12;
int miles = feet / 5280;
int remainingFeet = miles % feet;
int remainingInches = numOfInches % 12;
if (numOfInches > 0) {
System.out.println("\n\nMeasurement by combined miles, feet, inches:");
System.out.println("\tmiles: " + miles);
System.out.println("\tfeet: " + remainingFeet);
System.out.println("\tinches " + remainingInches);
System.out.println("\n\n" + numOfInches + " inches = " + miles
+ " miles, " + remainingFeet + " feet, " + remainingInches + " inches");
}
}
}
Try something like this:
if( numOfInches > 0 ) {
int inchesInFeet = 12;
int feetsInMile = 5280;
int inchesInMile = feetsInMile * inchesInFeet;
int miles = numOfInches / inchesInMile;
int remainingInches = numOfInches % inchesInMile;
int feet = remainingInches / inchesInFeet;
remainingInches = remainingInches % inchesInFeet;
System.out.println( "\n\nMeasurement by combined miles, feet, inches:" );
System.out.println( "\tmiles: " + miles );
System.out.println( "\tfeet: " + feet );
System.out.println( "\tinches " + remainingInches );
System.out.println( "\n\n" + numOfInches + " inches = " + miles
+ " miles, " + feet + " feet, " + remainingInches + " inches" );
}
Related
I'm trying to make a program that calculates tax and what change needs to be given back, but for some reason, it doesn't tell me what coins to give back. The code tells me which bills I would need to give back, but the coins that are in the decimals don't work. I've tried looking all over stack exchange for a solution, but if I'm being honest, I'm not really sure what to even look for in the first place, so I haven't found a solution. I'm also new to Java and text-based coding in general, so any feedback is appreciated!
Here is my code:
import java.util.*;
public class Decisions1 {
static String taskValue = "C1";
public static void main(String[] args) {
//initiates the scanner
Scanner myInput = new Scanner(System.in);
//creates the variables for the total
double total1 = 0;
//asks for the total cost of all goods
System.out.println("Please enter the total cost of all goods. This program will add tax and calculate the change you must give back to the customer.");
total1 = myInput.nextDouble();
//performs calculations for tax
double tax2 = (total1*1.13);
double tax1 = (double) Math.round(tax2 * 100) / 100;
//creates variable for amount paid
double paid1 = 0;
//prints info to the user and asks how much cash they received
System.out.println("You entered $" + total1 + "\n" + "Total with tax = $" + tax1 + "\n" + "Please enter how much cash was paid by the customer. The amount of change owed to the customer will be displayed.");
paid1 = myInput.nextDouble();
//checks if customer has paid enough
if (tax1 > paid1) {
double owed1 = (tax1-paid1);
System.out.println("The customer still owes $" + owed1 + "!");
} else if (tax1 == paid1) { //checks if the customer has given exact change
System.out.println("The customer has paid the exact amount. You don't have to give them any change.");
} else if (tax1 < paid1) {
//starts change calculations
//variables for money units
double oneHundred = 100;
double fifty = 50;
double twenty = 20;
double ten = 10;
double five = 5;
double toonie = 2;
double loonie = 1;
double quarter = 0.25;
double dime = 0.10;
double nickel = 0.05;
double penny = 0.01;
//variable for change owed (rounded to 2 decimal places
double change1 = Math.round((paid1-tax1)*100)/100;
//calculates remainder
double oneHundredMod = (change1 % oneHundred);
//divides and removes decimal places
double oneHundredOwed = (change1 / oneHundred);
String oneHundredOwed1 = String.valueOf((int) oneHundredOwed);
//does the same thing for fifty
double fiftyMod = (oneHundredMod % fifty);
double fiftyOwed = (oneHundredMod / fifty);
String fiftyOwed1 = String.valueOf((int) fiftyOwed);
//twenties
double twentyMod = (fiftyMod % twenty);
double twentyOwed = (fiftyMod / twenty);
String twentyOwed1 = String.valueOf((int) twentyOwed);
//tens
double tenMod = (twentyMod % ten);
double tenOwed = (twentyMod / ten);
String tenOwed1 = String.valueOf((int) tenOwed);
//fives
double fiveMod = (tenMod % five);
double fiveOwed = (tenMod / five);
String fiveOwed1 = String.valueOf((int) fiveOwed);
//toonies
double tooniesMod = (fiveMod % toonie);
double tooniesOwed = (fiveMod / toonie);
String tooniesOwed1 = String.valueOf((int) tooniesOwed);
//loonies
double looniesMod = (tooniesMod % loonie);
double looniesOwed = (tooniesMod / loonie);
String looniesOwed1 = String.valueOf((int) looniesOwed);
//quarters
double quartersMod = (looniesMod % quarter);
double quartersOwed = (looniesMod / quarter);
String quartersOwed1 = String.valueOf((int) quartersOwed);
//dimes
double dimesMod = (quartersMod % dime);
double dimesOwed = (quartersMod / dime);
String dimesOwed1 = String.valueOf((int) dimesOwed);
//nickels
double nickelsMod = (dimesMod % nickel);
double nickelsOwed = (dimesMod / nickel);
String nickelsOwed1 = String.valueOf((int) nickelsOwed);
//and finally, pennies!
double penniesMod = (nickelsMod % penny);
double penniesOwed = (nickelsMod / penny);
String penniesOwed1 = String.valueOf((int) penniesOwed);
System.out.println("TOTAL CHANGE:" + "\n" + "\n" + "$100 Bill(s) = " + oneHundredOwed1 + "\n" + "$50 Bill(s) = " + fiftyOwed1 + "\n" + "$20 Bill(s) = " + twentyOwed1 + "\n" + "$10 Bill(s) = " + tenOwed1 + "\n" + "$5 Bill(s) = " + fiveOwed1 + "\n" + "Toonie(s) = " + tooniesOwed1 + "\n" +"Loonie(s) = " + looniesOwed1 + "\n" + "Quarter(s) = " + quartersOwed1 + "\n" + "Dime(s) = " + dimesOwed1 + "\n" + "Nickel(s) = " + nickelsOwed1 + "\n" + "Pennies = " + penniesOwed1);
}
}//end main
}// end Decisions1
It is meant to calculate interest and then put that into a table with years, interest, and new balance. For some reason the interest is not being calculated correctly and is not updating.
import java.util.Scanner;
import java.text.*;
public class Interest
{
public static void main(String[] args)
{
printIntro();
Scanner input = new Scanner(System.in);
System.out.print("Enter initial balance: ");
int balanceAmount = input.nextInt();
System.out.print("Enter interest rate: ");
double interestRate = input.nextDouble();
System.out.print("Enter the number of years: ");
int years = input.nextInt();
printTable(years, balanceAmount, interestRate);
}
public static double calcInterest(double balanceAmount, double interestRate, double years)
{
double interest = balanceAmount * Math.pow((1 + interestRate/100),years);
return interest;
}
public static void printRow(int rowNum, double balanceAmount, double interestRate)
{
System.out.println(rowNum + "\t" + balanceAmount + "\t" + "\t" + interestRate + "\t" + "\t" + (balanceAmount + interestRate));
//balanceAmount = (balanceAmount + interestRate);
}
public static void printTable(int numRows, double balanceAmount, double interestRate)
{
System.out.println("Year" + "\t" + "Balance" + "\t" + "\t" + "Interest" + "\t" + "New Balance");
System.out.println("----" + "\t" + "-------" + "\t" + "\t" + "--------" + "\t" + "-----------");
for (int i = 1; i <= numRows; i++)
{
printRow(i, balanceAmount, interestRate);
balanceAmount = (balanceAmount + interestRate);
}
}
public static void printIntro()
{
System.out.println("This program will calculate the interest "
+ "earned on a deposit over a certain amount of years.");
}
}
You should call your business logic to calculate interest as per your business requirement. That totaly depend on your business requirement.
Though for program specific it seems that You need to call your calcInterest method in printTable method, before you call your printRow method as below:
public static void printTable( final int numRows, double balanceAmount, final double interestRate ) {
System.out.println( "Year" + "\t" + "Balance" + "\t" + "\t" + "Interest" + "\t" + "New Balance" );
System.out.println( "----" + "\t" + "-------" + "\t" + "\t" + "--------" + "\t" + "-----------" );
for ( int i = 1; i <= numRows; i++ ) {
double interest = calcInterest( balanceAmount, interestRate, 1 );
printRow( i, balanceAmount, interest );
balanceAmount = ( balanceAmount + interest );
}
}
Also your formula to calculate interest is wrong. it should be
double interest = balanceAmount * Math.pow( ( 1 + interestRate / 100 ), years )-balanceAmount;
It will give out put as below:
This program will calculate the interest earned on a deposit over a certain amount of years.
This program will calculate the interest earned on a deposit over a certain amount of years.
Enter initial balance: 100
Enter interest rate: 10
Enter the number of years: 3
Year Balance Interest New Balance
---- ------- -------- -----------
1 100.0 10.000000000000014 110.00000000000001
2 110.00000000000001 23.100000000000037 133.10000000000005
3 133.10000000000005 44.05610000000007 177.15610000000012
You are not calling calcInterest.
You need to call that under the printRow method before the
System.out.println(rowNum + "\t" + balanceAmount + "\t" + "\t" + interestRate + "\t" + "\t" + (balanceAmount + interestRate));
line
Entry level java program that won't compile. Any help at all would be amazing! I really just need a quick review of my basic level code. I get errors for ints, booleans, and doubles. I've tried to rearrange them in multiple ways and i just can not get it to work. Also my Cheesy crust calculation to add it the total. Again super novice but would really enjoy the help!
import java.util.Scanner;
public class programmingassignment1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner( System.in );
int pizzaLength;
int pizzaWidth;
int pizzaShape;
double pizzaDiamiter;
double pizzaToppingPrice = 0.025;
int numberOfPizzaToppings;
double pizzaSauceAndCheese = 0.036;
double pizzaDoughPrice = 0.019;
char doughType;
String dough;
int numberOfPizzas;
double pizzasBeforeTax;
int cheesyCrustInput;
int deliveryWantedInput;
int deliveryCharge;
double doughVolume;
double area;
double crustCost;
int basePrice;
int costOfPizzaWithTax;
//Shape of pizza
System.out.println("What shape of pizza requested?");
System.out.println("Enter (1) for Rectange or (2) Round?");
pizzaShape = keyboard.nextLine().charAt(0);
if(pizzaShape == 1){
System.out.println("What Length?");
pizzaLength = keyboard.nextInt();
System.out.println("What Width?");
pizzaWidth = keyboard.nextInt();
area = pizzaLength * pizzaWidth;
}
else if(pizzaShape == 2){
System.out.println("What is the Diamiter?");
pizzaDiamiter = keyboard.nextInt();
area = Math.PI * (pizzaDiamiter / 2);
}
//Volume
System.out.print("What type of dough do you want? (1)Classic Hand-Tossed, (2)Thin and Crispy, (3)Texas Toast, or (4)Pan. (enter 1,2,3, or 4.");
doughType = keyboard.nextLine().charAt(0);
if (doughType == 1) {
dough = "Classic Hand-Tossed";
doughVolume = area * .25;
}
else if (doughType == 2) {
dough = "Thin and Crispy";
doughVolume = area * .1;
}
else if (doughType == 3) {
dough = "Texas Toast";
doughVolume = area * .9;
}
else if (doughType == 4) {
dough = "Pan";
doughVolume = area * .5;
}
//Cheesey crust
if(doughType == 2){
}
else{
System.out.println("Would you like cheesy crust? (1) for yes (2) for no");
cheesyCrustInput = keyboard.nextInt();
if(cheesyCrustInput == 1){
crustCost = area * .02;
String Crust = "";
}
else{
String Crust = "NO";
}
}
//Toppings
System.out.print("Enter the number of toppings:");
numberOfPizzaToppings = keyboard.nextInt();
if(numberOfPizzaToppings == 0){
String toppings = "no";
}
else{
int toppings = numberOfPizzaToppings;
}
//Base price
basePrice = area (pizzaSauceAndCheese + (pizzaToppingPrice * numberOfPizzaToppings) + (pizzaDoughPrice * doughVolume));
//how many pizzas
System.out.print("Enter the number of pizzas being ordered:");
numberOfPizzas = keyboard.nextInt();
pizzasBeforeTax = basePrice * numberOfPizzas;
//tax
costOfPizzaWithTax = pizzasBeforeTax ( 1 + 0.07);
//delivery fee
System.out.print("Would you like delivery? (1) for yes, (2) for no.");
deliveryWantedInput = keyboard.nextInt();
if(deliveryWantedInput == 1){
String deliveryOutput = numberOfPizzas + "deliverd";
if(costOfPizzaWithTax < 10){
deliveryCharge = 3;
}
if( 10 < costOfPizzaWithTax && costOfPizzaWithTax < 20 ){
deliveryCharge = 2;
}
if(20 < costOfPizzaWithTax && costOfPizzaWithTax < 30){
deliveryCharge = 1;
}
if(costOfPizzaWithTax > 30){
deliveryCharge = 0;
}
}
else{
String deliveryOutput = "no delivery";
}
//display total
total = costOfPizzaWithTax + deliveryCharge;
System.out.println("Total Due: $" + df.format(total));
if(pizzaShape = 1){
System.out.print("User requested: Rectangular, " + pizzaWidth + "X" + pizzaLength + ", " + dough + toppings + "topping(s)"
+ Crust + "cheesy crust," + deliveryOutput + " - Program Output:");
System.out.println("Details for - Rectangular Pizza (" + pizzaWidth + "\" X " + pizzaLength + "\"):");
System.out.println( "Area: " + area );
System.out.println( "Volume:" + doughVolume);
System.out.println( "Base price: $" + basePrice);
System.out.println( "With Cheesy: $");
System.out.println( "Multi Total: $" + pizzasBeforeTax);
System.out.println( "With Tax: $" + costOfPizzaWithTax);
System.out.println( "And Delivery: $" + total);
}
else{
System.out.print("User requested: Circular, " + pizzaDiamiter + "\", " + dough + toppings + "topping(s)"
+ Crust + "cheesy crust," + deliveryOutput + " - Program Output:");
System.out.println( "Area: " + area );
System.out.println( "Volume:" + doughVolume);
System.out.println( "Base price: $" + basePrice);
System.out.println( "With Cheesy: $");
System.out.println( "Multi Total: $" + pizzasBeforeTax);
System.out.println( "With Tax: $" + costOfPizzaWithTax);
System.out.println( "And Delivery: $" + total);
}
}
}
Fixed content follows below!
import java.util.Scanner;
public class programmingassignment1 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
final double PIZZA_TOPPING_PRICE = 0.025;
final double PIZZA_SAUCE_AND_CHEESE = 0.036;
final double PIZZA_DOUGH_PRICE = 0.019;
double numberOfPizzaToppings;
double crustCost;
double basePrice;
double radiusOfPizza;
double sizeOfPizzaCrust;
double pizzasBeforeTax;
double pizzaLength = 1;
double pizzaWidth = 1;
double pizzaShape = 1;
double pizzaDiamiter = 0;
double doughType = 1;
double numberOfPizzas = 0;
double cheesyCrustInput = 0;
double deliveryWantedInput = 0;
double deliveryCharge = 0;
double doughVolume = 1;
double area = 1;
double costOfPizzaWithTax = 1;
double total = 1;
double toppings = 1;
String dough = "";
String crust = "";
String deliveryOutput;
String cheesyOutput = "";
// Shape of pizza
System.out.println("What shape of pizza requested?");
System.out.println("Enter (1) for Rectangle or (2) Round:");
pizzaShape = keyboard.nextDouble();
if (pizzaShape == 1) {
System.out.println("What Length?");
pizzaLength = keyboard.nextDouble();
System.out.println("What Width?");
pizzaWidth = keyboard.nextDouble();
area = pizzaLength * pizzaWidth;
} else if (pizzaShape == 2) {
System.out.println("What is the Diamiter?");
pizzaDiamiter = keyboard.nextDouble();
radiusOfPizza = pizzaDiamiter / 2;
area = Math.PI * radiusOfPizza * radiusOfPizza;
}
// Volume
System.out.print("What type of dough do you want? (1)Classic Hand Tossed,"
+ " (2)Thin and Crispy, (3)Texas Toast, or (4)Pan. \n(Enter 1,2,3, or 4.)");
doughType = keyboard.nextDouble();
if (doughType == 1) {
dough = "Classic Hand Tossed";
doughVolume = area * .25;
} else if (doughType == 2) {
dough = "Thin and Crispy";
doughVolume = area * .1;
} else if (doughType == 3) {
dough = "Texas Toast";
doughVolume = area * .9;
} else if (doughType == 4) {
dough = "Pan";
doughVolume = area * .5;
}
// Toppings
System.out.print("Enter the number of toppings:");
numberOfPizzaToppings = keyboard.nextDouble();
toppings = numberOfPizzaToppings;
// Base price
basePrice = area * (PIZZA_SAUCE_AND_CHEESE + PIZZA_TOPPING_PRICE * numberOfPizzaToppings)
+ PIZZA_DOUGH_PRICE * doughVolume;
// Cheesey crust
if (doughType == 2) {
cheesyOutput = " no cheesy crust";
crustCost = basePrice;
} else {
System.out.println("Would you like cheesy crust? (1) for yes (2) for no.");
cheesyCrustInput = keyboard.nextDouble();
if (cheesyCrustInput == 1 && pizzaShape == 2) {
sizeOfPizzaCrust = 2 * Math.PI * (pizzaDiamiter / 2);
crustCost = (sizeOfPizzaCrust * .02) + basePrice;
crust = "";
cheesyOutput = " cheesy crust";
} else if (cheesyCrustInput == 1 && pizzaShape == 1) {
sizeOfPizzaCrust = 2 * (pizzaLength + pizzaWidth);
crustCost = (sizeOfPizzaCrust * .02) + basePrice;
crust = "";
cheesyOutput = " cheesy crust";
} else {
crust = " NO cheesy crust";
crustCost = basePrice;
}
}
// how many pizzas
System.out.print("Enter the number of pizzas being ordered:");
numberOfPizzas = keyboard.nextDouble();
pizzasBeforeTax = crustCost * numberOfPizzas;
// tax
costOfPizzaWithTax = pizzasBeforeTax * (1 + 0.07);
// delivery fee
System.out.print("Would you like delivery? (1) for yes, (2) for no.");
deliveryWantedInput = keyboard.nextDouble();
if (deliveryWantedInput == 1) {
deliveryOutput = (int) numberOfPizzas + " pizza delivered";
if (costOfPizzaWithTax < 10) {
deliveryCharge = 3;
} else if (10 < costOfPizzaWithTax && costOfPizzaWithTax < 20) {
deliveryCharge = 2;
} else if (20 < costOfPizzaWithTax && costOfPizzaWithTax < 30) {
deliveryCharge = 1;
} else if (costOfPizzaWithTax > 30) {
deliveryCharge = 0;
}
} else {
deliveryOutput = "no delivery";
deliveryCharge = 0;
}
// display total
total = costOfPizzaWithTax + deliveryCharge;
if (pizzaShape == 1) {
System.out.print("User requested: Rectangular, " + (int) pizzaLength + "X"
+ (int) pizzaWidth + ", " + dough
+ ", " + (int) toppings + " topping(s)," + crust + cheesyOutput + ", "
+ deliveryOutput + " - Program Output:");
System.out.println("\nDetails for - Rectangular Pizza (" + pizzaLength + "\" X "
+ pizzaWidth + "\"):");
System.out.printf("Area: %.3f\n", area);
System.out.printf("Volume: %.3f\n", doughVolume);
System.out.printf("Base price: $%.2f", basePrice);
System.out.printf("\nWith Cheesy: $%.2f", crustCost);
System.out.printf("\nMulti Total: $%.2f", pizzasBeforeTax);
System.out.printf("\nWith Tax: $%.2f", costOfPizzaWithTax);
System.out.printf("\nAnd Delivery: $%.2f", total);
}
else {
System.out.print("User requested: Circular, " + (int) pizzaDiamiter + "\", "
+ dough + ", " + (int) toppings + " topping(s)," + crust + cheesyOutput
+ ", " + deliveryOutput + " - Program Output:");
System.out.println("\nDetails for - Round Pizza (" + pizzaDiamiter + "\" diameter):");
System.out.printf("Area: %.3f\n", area);
System.out.printf("Volume: %.3f\n", doughVolume);
System.out.printf("Base price: $%.2f", basePrice);
System.out.printf("\nWith Cheesy: $%.2f", crustCost);
System.out.printf("\nMulti Total: $%.2f", pizzasBeforeTax);
System.out.printf("\nWith Tax: $%.2f", costOfPizzaWithTax);
System.out.printf("\nAnd Delivery: $%.2f", total);
}
}
}
Your problem is related to scopes:
if ( condition ) {
String topping = "topping";
}
System.out.println(topping);
Will not compile. (Your code is a bit more broad, but still)
The problem here is that topping is a local variable in the if-block, once the if-block is terminated, the variable doesn't exist anymore.
You need to fix it to something like:
String topping = "default";
if ( condition ) {
topping = "topping";
}
System.out.println(topping);
Here is the error free code you need. You just need to create the methods and write your logic in it.
import java.util.Scanner;
public class pizzas {
public static void main(String[] args) {
Scanner keyboard = new Scanner( System.in );
int pizzaLength = 0;
int pizzaWidth = 0;
int pizzaShape;
double pizzaDiamiter = 0;
double pizzaToppingPrice = 0.025;
int numberOfPizzaToppings;
double pizzaSauceAndCheese = 0.036;
double pizzaDoughPrice = 0.019;
char doughType;
String dough = null;
int numberOfPizzas;
double pizzasBeforeTax;
int cheesyCrustInput;
int deliveryWantedInput;
int deliveryCharge = 0;
double doughVolume = 0;
double area = 0;
double crustCost;
int basePrice;
int costOfPizzaWithTax;
//Shape of pizza
System.out.println("What shape of pizza requested?");
System.out.println("Enter (1) for Rectange or (2) Round?");
pizzaShape = keyboard.nextLine().charAt(0);
if(pizzaShape == 1){
System.out.println("What Length?");
pizzaLength = keyboard.nextInt();
System.out.println("What Width?");
pizzaWidth = keyboard.nextInt();
area = pizzaLength * pizzaWidth;
}
else if(pizzaShape == 2){
System.out.println("What is the Diamiter?");
pizzaDiamiter = keyboard.nextInt();
area = Math.PI * (pizzaDiamiter / 2);
}
//Volume
System.out.print("What type of dough do you want? (1)Classic Hand-Tossed, (2)Thin and Crispy, (3)Texas Toast, or (4)Pan. (enter 1,2,3, or 4.");
doughType = keyboard.nextLine().charAt(0);
String Crust = "";
String deliveryOutput="";
if (doughType == 1) {
dough = "Classic Hand-Tossed";
doughVolume = area * .25;
}
else if (doughType == 2) {
dough = "Thin and Crispy";
doughVolume = area * .1;
}
else if (doughType == 3) {
dough = "Texas Toast";
doughVolume = area * .9;
}
else if (doughType == 4) {
dough = "Pan";
doughVolume = area * .5;
}
//Cheesey crust
if(doughType == 2){
}
else{
System.out.println("Would you like cheesy crust? (1) for yes (2) for no");
cheesyCrustInput = keyboard.nextInt();
if(cheesyCrustInput == 1){
crustCost = area * .02;
Crust = "";
}
else{
Crust = "NO";
}
}
//Toppings
System.out.print("Enter the number of toppings:");
numberOfPizzaToppings = keyboard.nextInt();
int toppings;
if(numberOfPizzaToppings == 0){
toppings = 0;
}
else{
toppings = numberOfPizzaToppings;
}
//Base price
basePrice = area (pizzaSauceAndCheese + (pizzaToppingPrice * numberOfPizzaToppings) + (pizzaDoughPrice * doughVolume));
//how many pizzas
System.out.print("Enter the number of pizzas being ordered:");
numberOfPizzas = keyboard.nextInt();
pizzasBeforeTax = basePrice * numberOfPizzas;
//tax
costOfPizzaWithTax = pizzasBeforeTax ( 1 + 0.07);
//delivery fee
System.out.print("Would you like delivery? (1) for yes, (2) for no.");
deliveryWantedInput = keyboard.nextInt();
if(deliveryWantedInput == 1){
deliveryOutput = numberOfPizzas + "deliverd";
if(costOfPizzaWithTax < 10){
deliveryCharge = 3;
}
if( 10 < costOfPizzaWithTax && costOfPizzaWithTax < 20 ){
deliveryCharge = 2;
}
if(20 < costOfPizzaWithTax && costOfPizzaWithTax < 30){
deliveryCharge = 1;
}
if(costOfPizzaWithTax > 30){
deliveryCharge = 0;
}
}
else{
deliveryOutput = "no delivery";
}
//display total
int total = costOfPizzaWithTax + deliveryCharge;
//System.out.println("Total Due: $" + df.format(total));
if(pizzaShape == 1){
System.out.print("User requested: Rectangular, " + pizzaWidth + "X"
+ pizzaLength + ", " + dough + toppings + "topping(s)"
+ Crust + "cheesy crust," + deliveryOutput + " - Program Output:");
System.out.println("Details for - Rectangular Pizza (" + pizzaWidth + "\" X " + pizzaLength + "\"):");
System.out.println( "Area: " + area );
System.out.println( "Volume:" + doughVolume);
System.out.println( "Base price: $" + basePrice);
System.out.println( "With Cheesy: $");
System.out.println( "Multi Total: $" + pizzasBeforeTax);
System.out.println( "With Tax: $" + costOfPizzaWithTax);
System.out.println( "And Delivery: $" + total);
}
else{
System.out.print("User requested: Circular, " + pizzaDiamiter + "\", " + dough + toppings + "topping(s)"
+ Crust + "cheesy crust," + deliveryOutput + " - Program Output:");
System.out.println( "Area: " + area );
System.out.println( "Volume:" + doughVolume);
System.out.println( "Base price: $" + basePrice);
System.out.println( "With Cheesy: $");
System.out.println( "Multi Total: $" + pizzasBeforeTax);
System.out.println( "With Tax: $" + costOfPizzaWithTax);
System.out.println( "And Delivery: $" + total);
}
}
Methods:
private static int pizzasBeforeTax(double d) {
// TODO Auto-generated method stub
return 0;
}
private static int area(double d) {
// TODO Auto-generated method stub
return 0;
}
Issues in your code:
1. You have not initialized the variables you have used.
2. You have declared the variables in if and else clause. When you are planning to re-use the variables, never initialize them inside as their scope is restricted to the block.
3. Also, you have used the same variable name toppings as String and int .
I want max and min values of salary to display but i get same values for max and min. Here's my code:
import java.util.*;
class Wage {
String employee_name, skill;
int hours;
double sum;
String[] sno = {"1", "2", "3", "4"};
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
Scanner s = new Scanner(System.in);
public void getEmployeeDetails() {
System.out.println("Welcome to Use General Wage Record System");
for (String count : sno) {
if (count.equalsIgnoreCase("1")) {
System.out.print("Enter Name of Employee 1: ");
employee_name = s.nextLine();
System.out.print("Enter the Skill Level (1,2,3) of Employee:");
Integer level_count = Integer.valueOf(s.nextLine());
if (level_count <= 3) {
System.out.print("Enter the Worked Hours for " + employee_name + ":");
hours = Integer.parseInt(s.nextLine());
if (level_count == 1) {
sum = hours * 15;
}
if (level_count == 2) {
sum = hours * 17;
}
if (level_count == 3) {
sum = hours * 21;
}
System.out.println("The wage of employee" + employee_name + "(Level" + String.valueOf(level_count) + ")" + "for" + hours + " " + "hours is" + " " + "$" + sum);
}
}
if (count.equalsIgnoreCase("2")) {
System.out.print("Enter Name of Employee 2:");
employee_name = s.nextLine();
System.out.print("Enter the Skill Level (1,2,3) of Employee:");
Integer level_count = Integer.valueOf(s.nextLine());
if (level_count <= 3) {
System.out.print("Enter the Worked Hours for " + employee_name + ":");
hours = Integer.parseInt(s.nextLine());
if (level_count == 1) {
sum = hours * 15;
}
if (level_count == 2) {
sum = hours * 17;
}
if (level_count == 3) {
sum = hours * 21;
}
System.out.println("The wage of employee " + employee_name + "(Level " + String.valueOf(level_count) + ")" + "for" + hours + " " + "hours is" + " " + "$" + sum);
}
}
if (count.equalsIgnoreCase("3")) {
System.out.print("Enter Name of Employee 3:");
employee_name = s.nextLine();
System.out.print("Enter the Skill Level (1,2,3) of Employee:");
Integer level_count = Integer.valueOf(s.nextLine());
if (level_count <= 3) {
System.out.print("Enter the Worked Hours for " + employee_name + ":");
hours = Integer.parseInt(s.nextLine());
if (level_count == 1) {
sum = hours * 15;
}
if (level_count == 2) {
sum = hours * 17;
}
if (level_count == 3) {
sum = hours * 21;
}
System.out.println("The wage of employee" + employee_name + "(Level" + String.valueOf(level_count) + ")" + "for" + hours + " " + "hours is" + " $" + sum);
}
}
if (count.equalsIgnoreCase("4")) {
System.out.print("Enter Name of Employee 4:");
employee_name = s.nextLine();
System.out.print("Enter the Skill Level (1,2,3) of Employee:");
Integer level_count = Integer.valueOf(s.nextLine());
if (level_count <= 3) {
System.out.print("Enter the Worked Hours for " + employee_name + ":");
hours = Integer.parseInt(s.nextLine());
if (level_count == 1) {
sum = hours * 15;
}
if (level_count == 2) {
sum = hours * 17;
}
if (level_count == 3) {
sum = hours * 21;
}
System.out.println("The wage of employee" + employee_name + "(Level" + String.valueOf(level_count) + ")" + "for" + hours + " " + "hours is" + "$ " + sum);
}
}
}
}
void average() {
System.out.println("\n\n");
System.out.println("stastical information for bar chart");
System.out.println("==================================");
if (sum > max) {
max = sum;
System.out.println("Maximum of wage" + max + ", " + employee_name);
}
if (sum < min) {
min= sum ;
System.out.println("Minimum of Wage" + min + ", " + employee_name);
}
}
}
public class Pay {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Wage wm = new Wage();
wm.getEmployeeDetails();
wm.average();
}
In the loop of the getEmployeeDetails() method, you don't store and update the min wage, the max wage and the employee names that have these wages.
Besides, you should be more specific in the naming. For example, maxWage and minWage are more meaningful than max and min.
So, you should have maxWage, maxWageEmployeeName, minWage and minWageEmployeeName fields in the field declaration of the Wage class.
At each employee inserted in the system, you should update these values by calling a dedicated method:
public void getEmployeeDetails() {
if (count.equalsIgnoreCase("1")) {
....
updateRanking(sum, employee_name);
}
}
public void updateRanking(double actualWage, String employee_name){
if (actualWage > maxWage) {
maxWage = actualWage;
maxWageEmployeeName = employee_name;
}
else if (actualWage < minWage) {
minWage = actualWage;
minWageEmployeeName = employee_name;
}
}
Besides, your average() method called at the end of your program should only display the result and not performing comparison since you should have updated information about max, min wage and who have these as updateRanking() is called at each insertion of employee wage :
void average() {
System.out.println("\n\n");
System.out.println("stastical information for bar chart");
System.out.println("==================================");
System.out.println("Maximum of wage" + maxWage + ", " + maxWageEmployeeName );
System.out.println("Minimum of Wage" + minWage + ", " + minWageEmployeeName );
}
I am a beginner and not tried the following program which is giving me repeated output.. I have to end the program manually in eclipse. Not able to figure out the problem. Please advice. Any other tips are welcome.
import java.util.Scanner;
public class Sales_Amount {
public static void main(String[] args) {
final double Base_Salary = 25000;
double Commission = 0;
double Total_Salary;
double X;
double Y;
Scanner input = new Scanner(System. in );
System.out.print("Enter Sales Amount: ");
double Sales = input.nextDouble();
while (Commission < 25001) {
if (Sales <= 5000); {
Total_Salary = Base_Salary + (0.08 * Sales);
System.out.print(" Total Salary for " + Sales + "worth of Sales is: " + Total_Salary);
}
if (Sales > 5000 && Sales < 10001); {
X = Sales - 5000;
Total_Salary = Base_Salary + (0.08 * 5000) + (0.10 * X);
System.out.print(" Total Salary for " + Sales + "worth of Sales is: " + Total_Salary);
}
if (Sales > 10001); {
Y = Sales - 10000;
Total_Salary = Base_Salary + (.08 * 5000) + (.10 * 10000) + (.12 * Y);
System.out.print(" Total Salary for " + Sales + "worth of Sales is: " + Total_Salary);
}
}
}
}
Add commission++ before the end of the loop.