Week 4 Course
Week 4 Course
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$
//Compute discount
}
package lacture05example;
import java.util.Scanner;
//Compute discount
}
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 discount
}
//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();
15
EXAMPLE
Memory
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
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)
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';
33
THE STRING TYPE
String message = "Welcome to Java";
34
STRING CONCATENATION (+
OPERATOR)
// Three strings are concatenated
String message = "Welcome " + "to " + "Java";
35