Transcript Lab 2

CSI 1390: Introduction to Computers
TA: Tapu Kumar Ghose
Office: STE 5014
Office hours: Thursday 1300 – 1400hrs.
Email: [email protected]
Assignment
 Take the temperature of a day from the user in degree Celsius and
convert it into degree Fahrenheit by using the formula

F = 9/5 * C + 32
 Given the value of r and h, find the volume of a cylinder by using the
formula

V = pi * r * r * h (the value of pi can be obtained by using the Math class)
Solutions
 We will be using the method called readDouble() of the Keyboard.java
 In order to do so, we have to compile the Keyboard.java program
 The “bin” directory of java is located at C:\Program
Files\Java\jdk1.5.0_12\bin
 Save the Keyboard.java program in the above directory
 Go to the above directory by using command promt by using the
following steps:
 Open command promt and enter the following commands
 cd\
 cd Program Files\Java\jdk1.5.0_12\bin
 To compile the Keyboard.java program enter the following command
 javac Keyboard.java
Solutions (Contd…)
 Before starting writing the source code we always try to determine few
things



What would be the input(s) to the program?
What would be the output(s) to the program?
How would we calculate the output?
 Here, the input is a double (for containing the temperature in degree C).
 Output is also a double (that contains the temperature in degree F)
 The output of age will be calculated by using mathematical formula
Source code for problem 1
import java.io.*;
class prob1 {
public static void main(String[] args){
double C, F;
Keyboard KB = new Keyboard();
System.out.print("Enter the temperature in degree C: ");
C = KB.readDouble();
F = 9/5 * C + 32;
System.out.print(“The temperature in degree F: “ + F);
}
}
COMPILE AND RUN THE PROGRAM.
Source code for problem 2
import java.io.*;
Import java.util.*;
class prob2 {
public static void main(String[] args){
double r, h, v;
Keyboard KBR = new Keyboard();
System.out.println("Enter the radius: ");
r = KBR.readDouble();
System.out.println("Enter the height: ");
h = KBR.readDouble();
v = Math.PI * r * r * h;
System.out.print(“The volume of the cylinder: “ + v);
}
}
COMPILE AND RUN THE PROGRAM.
If…else statement
 The rule (syntax) of writing if…..else statement:
if(condition){
statement 1;
statement 2;
statement 3;
………….
}
else{
statement 1;
statement 2;
…………..
}
 If the condition is TRUE, then the statements of the if block will be
executed. If the condition is FALSE, then else block’s statements will
be executed
Example of If…else statement
 Write a program that will take an integer as input and determine
whether the input integer is even of odd.
class odd{
public static void main(String args[]){
int x;
Keyboard kb = new Keyboard();
if((x % 2) = = 0){
System.out.println(“The input is even”);
}
else{
System.out.println(“The input is odd”);
}
}
Inequalities






Less than:
<
Greater than:
>
Less than or equal to <=
Less than or equal to >=
Equality
==
Do not confuse with assignment and equality sign
 Assignment
= (single equal sign)
 x = 5 means assigning 5 to the variable x
Logical operators
 AND:
&&
 e.g. x is less than 10 and y is greater than 15:
(x < 10 && y > 15)
 OR:
||
 e.g. x is less than 10 or y is less than 25:
(x < 10 || y < 25)
 Not Equal to
!=
 e.g. x is not equal to 35:
x != 35
Example of If…else statement

Write a program that will take an integer as input and determine whether the digit is 0,1,2or 3
import.java.io.*;
class odd{
public static void main(String args[]){
int x;
Keyboard kb = new Keyboard();
x = kb.readInt();
if(x = = 0){
System.out.println(“ZERO”);
}
else if(x = = 1){
System.out.println(“ONE”);
}
else if(x = = 2){
System.out.println(“TWO”);
}
else if(x = = 3){
System.out.println(“THREE”);
}
else
System.out.println(“Not one, two or three”);
}
}
switch…case statement

Write a program that will take an integer as input and determine whether the digit is 0,1,2or 3
class odd{
public static void main(String args[]){
int x;
Keyboard kb = new Keyboard();
System.out.println(”Enter a digit: ”);
x = kb.readInt();
switch(x){
case 0:
System.out.println(“ZERO”);
break;
case 1:
System.out.println(“ONE”);
break;
case 2:
System.out.println(“TWO”);
break;
case 3:
System.out.println(“THREE”);
break;
default:
System.out.println(“None of them entered”);
}
}
}
switch-case statement
 The rule (syntax) of writing switch-case statement:
switch(control variable){
case value1: statement 1;
statement 2;
………..
break;
case value2: statement 1;
statement 2;
………..
break;
……………………………
case value n: statement 1;
statement 2;
………..
break;
default:
statements;
}
switch-case statement
 switch takes a conditional variable
 Each block begins with case keyword followed by
a value of the control variable
 Each block ends with break keyword
 Omission of the break keyword will result in
executing the following case block
 If none of the value of control variable is true, then
the statement(s) following the default keyword will
be executed.
Practice
 A triangle is said to be right triangle if the square
of the largest side is equal to the sum of the square
of the other two sides. For example, if x, y and z
are three sides of a triangle and x is the largest side,
then the triangle will be a right triangle if
x2 = y2 + z2
 Take three sides of a triangle from the user and
write program to determine whether the triangle
formed by these three sides is a right triangle or
not.