0% found this document useful (0 votes)
58 views35 pages

Week 4 Course

The document describes a Java program that takes in product information from a user including code, unit price, quantity, and discount rate. It then calculates the total price, discount amount, and final price and displays the results. The program uses a Scanner to read input, performs calculations, and outputs the results by printing to the console.

Uploaded by

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

Week 4 Course

The document describes a Java program that takes in product information from a user including code, unit price, quantity, and discount rate. It then calculates the total price, discount amount, and final price and displays the results. The program uses a Scanner to read input, performs calculations, and outputs the results by printing to the console.

Uploaded by

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

CHAPTER 2 Dr.

Ghalia Nasserddine
EXAMPLE
Write a program that read from user:
1. the product code (String) Sample Run:
Enter the product code: A100
2. the unit price of a product (double)
Enter the unit price:2,9
3. The quantity (int)
Enter the quantity: 10
4. The discount rate (double) Enter the discount rate: 10
The program should display the Total price=29$
total price=unit Price* Quantity Discount =2,9$

Discount= total price*(1-discount/100) Final Price= 26,1$

Final Price= total price –discount


package lacture05example;
public class Lacture05Example {
public static void main(String[] args) {
// read data (Code, unitPrice, Quantity and discount rate

//Compute total price

//Compute discount

//Compute final price


//Display results
}

}
package lacture05example;

import java.util.Scanner;

public class Lacture05Example {


public static void main(String[] args) {
// read data (Code, unitPrice, Quantity and discount rate
Scanner s=new Scanner(System.in);

//Compute total price

//Compute discount

//Compute final price


//Display results

}
package lacture05example;
import java.util.Scanner;
public class Lacture05Example {
public static void main(String[] args) {
// read data (Code, unitPrice, Quantity and discount rate
Scanner s=new Scanner(System.in);
System.out.print("Enter the product code: ");
String code=s.next();
System.out.print("Enter the unit price: ");
double unitPrice=s.nextDouble();
System.out.print("Enter the quantity: ");
int qty=s.nextInt();
System.out.print("Enter the discount rate: ");
double discount=s.nextDouble();

//Compute total price

//Compute discount

//Compute final price

}
//Compute total price
//Compute discount
//Compute final price
double totalPrice=qty*unitPrice;
double discountValue=totalPrice*discount/100;
double finalPrice=totalPrice-discountValue;
//display result
System.out.println("Total price: "+totalPrice+"$");
System.out.println("Discount: "+discountValue+"$");
System.out.println("Final Price: "+finalPrice+"$");
// read data (Code, unitPrice, Quantity and discount rate
Scanner s=new Scanner(System.in);
System.out.print("Enter the product code: ");
String code=s.next();
System.out.print("Enter the unit price: "); Output
double unitPrice=s.nextDouble();
Enter the product code:
System.out.print("Enter the quantity: ");
int qty=s.nextInt();
System.out.print("Enter the discount rate: ");
double discount=s.nextDouble();

//Compute total price Memory


//Compute discount
//Compute final price
double totalPrice=qty*unitPrice;
double discountValue=totalPrice*discount/100;
double finalPrice=totalPrice-discountValue;
//display result
System.out.println("Total price: "+totalPrice+"$");
System.out.println("Discount: "+discountValue+"$");
System.out.println("Final Price: "+finalPrice+"$");
// read data (Code, unitPrice, Quantity and discount rate
Scanner s=new Scanner(System.in);
System.out.print("Enter the product code: ");
String code=s.next();
System.out.print("Enter the unit price: "); Output
double unitPrice=s.nextDouble();
Enter the product code: RB100
System.out.print("Enter the quantity: ");
int qty=s.nextInt();
System.out.print("Enter the discount rate: ");
double discount=s.nextDouble();

//Compute total price Memory


//Compute discount code RB100
//Compute final price
double totalPrice=qty*unitPrice;
double discountValue=totalPrice*discount/100;
double finalPrice=totalPrice-discountValue;
//display result
System.out.println("Total price: "+totalPrice+"$");
System.out.println("Discount: "+discountValue+"$");
System.out.println("Final Price: "+finalPrice+"$");
// read data (Code, unitPrice, Quantity and discount rate
Scanner s=new Scanner(System.in);
System.out.print("Enter the product code: ");
String code=s.next();
System.out.print("Enter the unit price: "); Output
double unitPrice=s.nextDouble();
Enter the product code: RB100
System.out.print("Enter the quantity: ");
Enter the unit price: 3,5
int qty=s.nextInt();
System.out.print("Enter the discount rate: ");
double discount=s.nextDouble();

//Compute total price Memory


//Compute discount code RB100
//Compute final price
unitPrice 3,5
double totalPrice=qty*unitPrice;
double discountValue=totalPrice*discount/100;
double finalPrice=totalPrice-discountValue;
//display result
System.out.println("Total price: "+totalPrice+"$");
System.out.println("Discount: "+discountValue+"$");
System.out.println("Final Price: "+finalPrice+"$");
// read data (Code, unitPrice, Quantity and discount rate
Scanner s=new Scanner(System.in);
System.out.print("Enter the product code: ");
String code=s.next();
System.out.print("Enter the unit price: "); Output
double unitPrice=s.nextDouble();
Enter the product code: RB100
System.out.print("Enter the quantity: ");
Enter the unit price: 3,5
int qty=s.nextInt();
Enter the quantity: 5
System.out.print("Enter the discount rate: ");
double discount=s.nextDouble();

//Compute total price Memory


//Compute discount code RB100
//Compute final price
unitPrice 3,5
double totalPrice=qty*unitPrice;
double discountValue=totalPrice*discount/100; qty 5
double finalPrice=totalPrice-discountValue;
//display result
System.out.println("Total price: "+totalPrice+"$");
System.out.println("Discount: "+discountValue+"$");
System.out.println("Final Price: "+finalPrice+"$");
// read data (Code, unitPrice, Quantity and discount rate
Scanner s=new Scanner(System.in);
System.out.print("Enter the product code: ");
String code=s.next();
System.out.print("Enter the unit price: "); Output
double unitPrice=s.nextDouble();
Enter the product code: RB100
System.out.print("Enter the quantity: ");
Enter the unit price: 3,5
int qty=s.nextInt();
Enter the quantity: 5
System.out.print("Enter the discount rate: ");
Enter the discount rate: 20
double discount=s.nextDouble();

//Compute total price Memory


//Compute discount code RB100
//Compute final price
unitPrice 3,5
double totalPrice=qty*unitPrice;
double discountValue=totalPrice*discount/100; qty 5
double finalPrice=totalPrice-discountValue; discount 20
//display result
System.out.println("Total price: "+totalPrice+"$");
System.out.println("Discount: "+discountValue+"$");
System.out.println("Final Price: "+finalPrice+"$");
// read data (Code, unitPrice, Quantity and discount rate
Scanner s=new Scanner(System.in);
System.out.print("Enter the product code: ");
String code=s.next();
System.out.print("Enter the unit price: "); Output
double unitPrice=s.nextDouble();
Enter the product code: RB100
System.out.print("Enter the quantity: ");
Enter the unit price: 3,5
int qty=s.nextInt();
Enter the quantity: 5
System.out.print("Enter the discount rate: ");
Enter the discount rate: 20
double discount=s.nextDouble();

//Compute total price Memory


//Compute discount code RB100 totalPrice 17,5
//Compute final price
unitPrice 3,5 discountValue
double totalPrice=qty*unitPrice; 3,5
double discountValue=totalPrice*discount/100; qty 5 finalPrice 14
double finalPrice=totalPrice-discountValue; discount 20
//display result
System.out.println("Total price: "+totalPrice+"$");
System.out.println("Discount: "+discountValue+"$");
System.out.println("Final Price: "+finalPrice+"$");
// read data (Code, unitPrice, Quantity and discount rate
Scanner s=new Scanner(System.in); Output
System.out.print("Enter the product code: "); Enter the product code: RB100
String code=s.next(); Enter the unit price: 3,5
System.out.print("Enter the unit price: "); Enter the quantity: 5
double unitPrice=s.nextDouble(); Enter the discount rate: 20
System.out.print("Enter the quantity: "); Total Price: 17,5$
int qty=s.nextInt(); Discount: 3,5$
System.out.print("Enter the discount rate: "); Final Price: 14$
double discount=s.nextDouble();

//Compute total price Memory


//Compute discount code RB100 totalPrice 17,5
//Compute final price
unitPrice 3,5 discountValue
double totalPrice=qty*unitPrice; 3,5
double discountValue=totalPrice*discount/100; qty 5 finalPrice 14
double finalPrice=totalPrice-discountValue; discount 20
//display result
System.out.println("Total price: "+totalPrice+"$");
System.out.println("Discount: "+discountValue+"$");
System.out.println("Final Price: "+finalPrice+"$");
SHORTCUT
ASSIGNMENT
OPERATORS
Operator Example Equivalent
+= i += 8 i = i + 8
-= f -= 8.0 f = f - 8.0
*= i *= 8 i = i * 8
/= i /= 8 i = i / 8
%= i %= 8 i = i % 8

15
EXAMPLE
Memory

int x=8, y=-2, z=0;


x 8
x+=5; y -2
y*=x; z 0
z+=(y*2);
EXAMPLE
Memory

int x=8, y=-2, z=0;


x 8 13
x+=5; // x=x+5; y -2 -26
y*=x; //y=x*y; z 0 -52
z+=(y*2);// z=z+(y*2)
EXAMPLE
Memor
y
What is the output of the following
code: m -4
n
int m=-4, n=12, r=7; 12

m=r*2+n/12; r 7
outpu
m+=5; t
n*=10;
r/=2;

System.out.println("m= "+m+", n=
"+n+", r= "+r);
EXAMPLE
Memor
y
What is the output of the following
code: m -4 15 20
n
int m=-4, n=12, r=7; 12 120

m=r*2+n/12; r 73
outpu
m+=5;//m=m+5; t
n*=10;//n=n*10;
r/=2;//r=r/2;

System.out.println("m= "+m+", n=
"+n+", r= "+r);
EXAMPLE
Memor
y
What is the output of the following
code: m -4 15 20
n
int m=-4, n=12, r=7; 12 120

m=r*2+n/12; r 73
outpu
m+=5; t
n*=10; m=20, n=120, r=3

r/=2;

System.out.println("m= "+m+", n=
"+n+", r= "+r);
INCREMENT /
DECREMENT
++var preincrement
var++ postincrement

--var predecrement
var-- postdecrement

What is the difference between ++var and var++?

21
POSTINCREMENT AND
POSTDECREMENT
int x=10, y=2; Output:
x++;//x=x+1; 11
System.out.println(x); 1
y--;//y=y-1;
System.out.println(y);
PREINCREMENT AND
PREDECREMENT
int x=10, y=2; Output:
++x;//x=x+1; 11
System.out.println(x); 1
--y;//y=y-1;
System.out.println(y);
DIFFERENCE BETWEEN PRE
AND POST
outout
int x=10, y=2, z=0;
z=++x+y*2;
int w=2*(y--);
System.out.println("z: "+z+" w:
"+w); Memory

x 10 z 0

y 2
DIFFERENCE BETWEEN PRE
AND POST
outout
int x=10, y=2, z=0;
z=++x+y*2;
//x=x+1;
int w=2*(y--);
System.out.println("z: "+z+" w: Memory
"+w); x 10 11 z 0

y 2
DIFFERENCE BETWEEN PRE
AND POST
outout
int x=10, y=2, z=0;
z=++x+y*2;
//x=x+1;
//z=x+y*2=11+2*2=15
int w=2*(y--); Memory

x 10 11 z 0 15
System.out.println("z: "+z+" w:
"+w);
y 2 w
DIFFERENCE BETWEEN PRE
AND POST
outout
int x=10, y=2, z=0;
z=++x+y*2;
//x=x+1;
//z=x+y*2=11+2*2=15
int w=2*(y--); Memory

x 10 11 z 0 15
//w=2*y=2*2=4
System.out.println("z: "+z+" w: "+w); y 2 w 4
DIFFERENCE BETWEEN PRE
AND POST
outout
int x=10, y=2, z=0;
z: 15 w: 4
z=++x+y*2;
//x=x+1;
//z=x+y*2=11+2*2=15
int w=2*(y--); Memory

x 10 11 z 0 15
//w=2*y=2*2=4
//y=y-1; y 21 w 4

System.out.println("z: "+z+" w:
"+w);
TYPE CASTING
Implicit casting
double d = 3; (type widening)

Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (truncate decimal)
What is wrong? int x = 5 / 2.0;

29
CHARACTER DATA TYPE
char letter = 'A'; (ASCII)
char numChar = '4'; (ASCII)

NOTE: Increment (decrement) operators on char variables


get the next (preceding) Unicode character.
What character is printed by these statements?
char ch = 'a';
System.out.println(++ch);
30
ESCAPE SEQUENCES FOR
SPECIAL CHARACTERS
Description Escape Sequence Unicode
Backspace \b \u0008
Tab \t \u0009
Linefeed \n \u000A
Carriage return \r \u000D
Backslash \\ \u005C
Single Quote \' \u0027
Double Quote \" \u0022

31
APPENDIX B: ASCII
CHARACTER SET
ASCII Character Set is a subset of the Unicode from \u0000 to
\u007f

32
CASTING BETWEEN
CHAR AND NUMERIC
TYPES
int i = 'a'; // Same as int i = (int)'a';

char c = 97; // Same as char c = (char)97;

Unicode representation of characters

33
THE STRING TYPE
String message = "Welcome to Java";

Compare char and String:


char: single character
String: several characters
char literal ‘a’
String literal “A”
char is a primitive data type
String is predefined class (reference type)

More on classes later!

34
STRING CONCATENATION (+
OPERATOR)
// Three strings are concatenated
String message = "Welcome " + "to " + "Java";

// String Chapter is concatenated with number 2


String s = "Chapter" + 2; // s becomes Chapter2

// String Supplement is concatenated with character B


String s1 = "Supplement" + 'B'; // s becomes
SupplementB

35

You might also like