Lec2-Computer Graphics
Lec2-Computer Graphics
Java Applet
Lecture 2
i. Comparison Operators
ii. The if-else / switch-case Statement
iii. Logical Operators
iv. Loops
5
Using Intellij Idea
Intellij Idea is powerful IDE for Java and other languages
Create a project
Declaring Variables
Defining and Initializing variables
9
Console I/O
Reading from and Writing to the Console
10
Reading from the Console
We can read/write to the console,
using the Scanner class
Import the java.util.Scanner class
import java.util.Scanner;
…
Scanner sc = new Scanner(System.in);
12
Printing to the Console
We can print to the console, using the System class
Writing output to the console:
System.out.print()
System.out.println()
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.println("Hi, " + name);
// Name: George
// Hi, George
13
Using Print Format
Using format to print at the console
Examples:
String name = "George"; Placeholder %s stands
for string and
int age = 5; corresponds to name
System.out.printf("Name: %s, Age: %d", name, age);
// Name: George, Age: 5
Placeholder %d
stands for integer
number and
corresponds to age
14
Formatting Numbers in Placeholders
D – format number to certain digits with leading zeros
F – format floating point number with certain digits after the
decimal point
Examples:
int percentage = 55;
double grade = 5.5334;
System.out.printf("%03d", percentage); // 055
System.out.printf("%.2f", grade); // 5.53
Using String.format
Using String.format to create a string by pattern
Examples:
String name = "George";
int age = 5;
String result = String.format("Name: %s,
Age: %d", name, age);
System.out.println(result);
//Name: George, Age 5
Problem: Student Information
You will be given 3 input lines:
Student Name, Age and Average Grade
Print the input in the following format:
"Name: {name}, Age: {age}, Grade {grade}"
Format the grade to 2 decimal places
John
15 Name: John, Age: 15, Grade: 5.40
5.40
Solution: Student Information
import java.util.Scanner;
…
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
int age = Integer.parseInt(sc.nextLine());
double grade = Double.parseDouble(sc.nextLine());
11 12 11
08 11:38 49 13:19 12:02
32
Solution: I Will be Back in 30 Minutes (1)
Weekday Holiday
18$ Error!
42 -12
Solution: Theatre Promotions (1)
String day = sc.nextLine().toLowerCase();
int age = Integer.parseInt(sc.nextLine());
int price = 0;
if (day.equals("weekday")) {
if ((age >= 0 && age <= 18) || (age > 64 && age <= 122))
{
price = 12;
}
// TODO: Add else statement for the other group
}
// Continue…
Solution: Theatre Promotions (2)
else if (day.equals("weekend"))
{
if ((age >= 0 && age <= 18) || (age > 64 && age <= 122))
{ price = 15; }
else if (age > 18 && age <= 64) {
price = 20;
}
} // Continue…
Solution: Theatre Promotions (3)
else if (day.equals("holiday")){
if (age >= 0 && age <= 18)
price = 5;
// TODO: Add the statements for the other cases
}
if (age < 0 || age > 122)
System.out.println("Error!");
else
System.out.println(price + "$");
Loops
Code Block Repetition
Loop: Definition
A loop is a control statement that repeats
the execution of a block of statements. The loop can:
for loop
Execute a code block a fixed number of times
while and do…while
Execute a code block
while a given condition returns true
39
For-Loops
Managing the Count of the Iteration
For-Loops
The for loop executes statements a fixed number of times:
System.out.println(i);
}
1
3 1
5 3
5 3
7 5
9 Sum: 9
Sum: 25
Solution: Sum of Odd Numbers
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
int sum = 0;
for (int i = 1; i <= n*2; i=i+2)
{
System.out.println( i );
sum += i ;
}
System.out.printf("Sum: %d", sum);
}
}
Solution: Sum of Odd Numbers(Another solution)
int n = Integer.parseInt(sc.nextLine());
int sum = 0;
Initial value
Condition
int n = 1;
while (n <= 10) { Loop body
System.out.println(n);
n++;
} Increment the counter
Problem: Multiplication Table
Print a table holding number*1, number*2, …, number*10
int number = Integer.parseInt(sc.nextLine());
int times = 1;
while (times <= 10) {
System.out.printf("%d X %d = %d%n",
number, times, number * times);
times++;
}
Problem: Multiplication Table( Another solution)
Print a table holding number*1, number*2, …, number*10
int number = Integer.parseInt(sc.nextLine());
int times = 1;
while (times <= 10)
{System.out.println(number+ " X " + times+ " =
" + number*times);
times++ ;
}
Do…While Loop
Execute a Piece of Code One or More Times
Do ... While Loop
Similar to the while loop, but always executes at least once: