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

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)...
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...
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;    ...
JAVA: This is my code, but when it runs, for the "Average Score" output, it only...
JAVA: This is my code, but when it runs, for the "Average Score" output, it only gives me NaN. How can I fix that? import java.util.Scanner; public class prog4 { public static void main(String[] args) { Scanner reader = new Scanner(System.in); String name; double score; double minScore = 0; double maxScore = 0; int numberOfRecords = 0; double sum = 0; double average = sum / numberOfRecords; System.out.printf("%-15s %-15s %-15s\n", "Student#", "Name", "Score");           while (reader.hasNext()) { name...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner...
This for Java Programming Write a java statement to import the java utilities. Create a Scanner object to read input. int Age;     Write a java statement to read the Age input value 4 . Redo 1 to 3 using JOptionPane
This is my java method that is suppose to take data from a file(String, String, Double,...
This is my java method that is suppose to take data from a file(String, String, Double, Int ) and load that data into an object array. This is what I have and when I try to display the contents of this array it prints a "@" then some numbers and letters. which is not the correct output. any help correcting my method would be much appreciated. public void loadInventory(String fileName) { String inFileName = fileName; String id = null; String...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT