Question

In: Computer Science

in java my code double price[] = {26.99, 22.99, 13.99, 56.99, 38.99}; // Variable Declaration Scanner...

in java

my code

double price[] = {26.99, 22.99, 13.99, 56.99, 38.99};

// Variable Declaration

Scanner keyIn = new Scanner(System.in);

//Header

System.out.println( "----------------------------------------\n"

+ " Grocery Shop Price Calculator \n"

+ "----------------------------------------\n") ;

System.out.print ("Please enter the quantities for each item in the list? ");

double totalCost = 0;

double fishAmount = 0;

for(int i=0; i<price.length; i++)

{

int items = Integer.parseInt(keyIn.nextLine());

if(i==4)

fishAmount = price[i]*items;

else

totalCost = totalCost + price [i]*items;

}

System.out.print("Do you have the membership(Y/N) ");

String st = keyIn.nextLine();

double off;

if(totalCost<250)

off = totalCost*0.1;

else if(totalCost<500)

off = totalCost*0.2;

else

off = totalCost*0.15;

totalCost = totalCost + fishAmount - off;

long points = 0;

if(st.equalsIgnoreCase("Y"))

{

if(totalCost<500)

points = Math.round(totalCost*2);

else

points = Math.round(totalCost*3);

}

System.out.print("The total price is $"+ totalCost+".");

if(st.equalsIgnoreCase("Y"))

System.out.println("You will receive " + points + " points.");

//close scanner

keyIn.close();

System.out.println ("Thanks for shopping! See you next time!");

}

}

and it is not working second question

this-System.out.print("Do you have the membership(Y/N) ");

just come out first question and stop

what is my problem?

Solutions

Expert Solution

The program is written correctly except:

Line no 51: else if(totalCost>500)   //less than is changed to greater than ( logical error) because the 0.2 discount should be given to greater totalCost ie) greater than 500

Line no 1: //These lines are missing.

import java.util.Scanner;
  public class Main
{
   public static void main(String[] args) {

Output:

The expected output is achieved.

Program:

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
double price[] = {26.99, 22.99, 13.99, 56.99, 38.99};

// Variable Declaration

Scanner keyIn = new Scanner(System.in);

//Header

System.out.println( "----------------------------------------\n"

+ " Grocery Shop Price Calculator \n"

+ "----------------------------------------\n") ;

System.out.print ("Please enter the quantities for each item in the list? ");

double totalCost = 0;

double fishAmount = 0;

for(int i=0; i<price.length; i++)

{

int items = Integer.parseInt(keyIn.nextLine());

if(i==4)

fishAmount = price[i]*items;

else

totalCost = totalCost + (price [i]*items);

}

System.out.print("Do you have the membership(Y/N) ");

String st = keyIn.nextLine();

double off;

if(totalCost<250)

off = totalCost*0.1;

else if(totalCost>500) //less than is changed to greater than

off = totalCost*0.2;

else

off = totalCost*0.15;

totalCost = totalCost + fishAmount - off;

long points = 0;

if(st.equalsIgnoreCase("Y"))

{

if(totalCost<500)

points = Math.round(totalCost*2);

else

points = Math.round(totalCost*3);

}

System.out.print("The total price is $"+ totalCost+".");

if(st.equalsIgnoreCase("Y"))

System.out.println("You will receive " + points + " points.");

//close scanner

keyIn.close();

System.out.println ("Thanks for shopping! See you next time!");

}

}


Related Solutions

7) Create the following using Java. Create a scanner Declare double variables for price and tax...
7) Create the following using Java. Create a scanner Declare double variables for price and tax Declare character variable reply Create do while loop Inside of do loop Prompt the console to display headline Product Price Check Prompt the user to enter initial price and relate to scanner Prompt the user to enter the tax rate and relate to scanner Calculate price which is equal to price multiply (1+tax/100) Display in console the cost after tax which is price Check...
Given the following Java source code fragment double price[]; price = new double[16]; What's the name...
Given the following Java source code fragment double price[]; price = new double[16]; What's the name of the array? How many elements are in the array? What's the index of the first element in the array? What's the index of the last element in the array? What's the value of the element at index 3 after the last statement executes?
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int...
My current code that is not working correctly #include<stdio.h> #include<math.h> int main() { //variable declaration int a,b,c,D,n; double x1,x2; double realPart, imagPart; do { // this set of code blocks prompts a message to the user and then read the integer entered and stores it as an integer printf("Enter a value for a:\n"); scanf("%d",&a); printf("Enter a value for b:\n"); scanf("%d",&b); printf("Enter a value for c:\n"); scanf("%d",&c); printf("You entered the Equation \n"); printf("%dx^2%+dx%+d=0\n",a,b,c); D = b*b - 4*a*c;    if (D<0)...
8) Create the following program using Java. Circle calculation using methods Create scanner declare double variable...
8) Create the following program using Java. Circle calculation using methods Create scanner declare double variable radius = -999 declare character choice create do while loop inside of do loop write: System.out.println(); System.out.println("*** CIRCLE CALCULATIONS ***"); System.out.println(); System.out.println("1. Enter the radius of the circle"); System.out.println("2. Display the area of the circle"); System.out.println("3. Display the circumference of the circle"); System.out.println("4. Quit"); System.out.println(); System.out.println("Enter a number from 1 - 4"); System.out.println(); Declare choice character and relate to scanner declare switch (choice) case...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
In java, what does it mean when a variable "exists" from its declaration to }?
In java, what does it mean when a variable "exists" from its declaration to }?
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** *...
COMPLETE JAVA CODE public class Point2 { private double x; private double y;    /** * Create a point with coordinates <code>(0, 0)</code>. */ public Point2() { complete JAVA code this.set(0.0, 0.0); COMPLETE CODE }    /** * Create a point with coordinates <code>(newX, newY)</code>. * * @param newX the x-coordinate of the point * @param newY the y-coordinate of the point */ public Point2(double newX, double newY) { complete Java code this.set(newX, newY); }    /** * Create a...
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to...
CODE IN JAVA Create a new class named Task1. In its main function, use Scanner to read integers from the keyboard with the method nextInt. Use a try catch block to capture non-integer input, and tell the user, "This is not an Integer". The program loop should end when the user enters the string "quit". (Hint: "quit" is clearly not a number and will be captured in the try / catch block.) A typical Output Dialog is shown below: Enter...
in java: In my code at have my last two methods that I cannot exactly figure...
in java: In my code at have my last two methods that I cannot exactly figure out how to execute. I have too convert a Roman number to its decimal form for the case 3 switch. The two methods I cannot figure out are the public static int valueOf(int numeral) and public static int convertRomanNumber(int total, int length, String numeral). This is what my code looks like so far: public static void main(String[] args) { // TODO Auto-generated method stub...
In Java, please write a tester code. Here's my code: public class Bicycle {     public...
In Java, please write a tester code. Here's my code: public class Bicycle {     public int cadence; public int gear;   public int speed;     public Bicycle(int startCadence, int startSpeed, int startGear) {         gear = startGear;   cadence = startCadence; speed = startSpeed;     }     public void setCadence(int newValue) {         cadence = newValue;     }     public void setGear(int newValue) {         gear = newValue;     }     public void applyBrake(int decrement) {         speed -= decrement;    ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT