0% found this document useful (0 votes)
37 views

Assignment 6

The document contains code for two Java programs. Program 1 uses a do-while loop to calculate the total, average, number of positives and negatives of a series of integer inputs. Program 2 uses a while loop and switch statement to calculate the total price of different products based on product number and quantity input. It prompts the user to input a product number and quantity, uses a switch statement to calculate the price for that product, and repeats until the user inputs -9999 to exit and displays the total price.

Uploaded by

Yang Fang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Assignment 6

The document contains code for two Java programs. Program 1 uses a do-while loop to calculate the total, average, number of positives and negatives of a series of integer inputs. Program 2 uses a while loop and switch statement to calculate the total price of different products based on product number and quantity input. It prompts the user to input a product number and quantity, uses a switch statement to calculate the price for that product, and repeats until the user inputs -9999 to exit and displays the total price.

Uploaded by

Yang Fang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment 6

Q1:
import java.util.Scanner;

public class Assignment6 {


public static void main(String [] args) {
Scanner s = new Scanner(System.in);
int positive = 0;
int negative = 0;
double total = 0;
int count = 0;
double average;
int number;
System.out.println("Enter number");

do { number = s.nextInt();
total += number;
count++;
if(number > 0){
positive++;
}
else if(number < 0){
negative++;
}
}
while(number != 0);

average = total / count;

System.out.println("The number of positives is "+ positive);


System.out.println("The number of negatives is "+ negative);
System.out.println("The total is "+ total);
System.out.println("The average is "+ average);

}
Q2:

Algorithm:
Start
Enter product number
Enter quantity sold
Total price 1= price 1of number *quantity sold
Total price 2= price 2of number *quantity sold
Total price 3= price 3of number *quantity sold
Total price 4= price 4of number *quantity sold
Total price 5= price 5of number *quantity sold

Print total price


End

Code;
import java.util.Scanner;

public class Assignment6 二 {


public static void main (String [] args) {
Scanner s = new Scanner(System.in);
double totalprice = 0;

while (true) {
System.out.println("price of product 1 is RM2.98");
System.out.println("price of product 2 is RM4.5");
System.out.println("price of product 3 is RM9.98");
System.out.println("price of product 4 is RM4.49");
System.out.println("price of product 5 is RM6.87");
System.out.println("-9999 out of programm");
System.out.println("Enter product number");
int p= s.nextInt();
if(p == -9999)
break;}
System.out.println("Enter quantity sold");
int q = s.nextInt();
switch('p') {
case 1:
totalprice+=2.98*q;
break;
case 2:
totalprice+=4.50*q;
break;
case 3:
totalprice+=9.98*q;
break;
case 4:
totalprice+=4.49*q;
break;
case 5:
totalprice+=6.87*q;
break;}
System.out.println("The total retail value of all
products sold: $"+totalprice);

You might also like