1 / 15

CSI 1390: Introduction to Computers

CSI 1390: Introduction to Computers. TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs. Email: tghos009@uottawa.ca. Assignment. Take the temperature of a day from the user in degree Celsius and convert it into degree Fahrenheit by using the formula

byrnes
Download Presentation

CSI 1390: Introduction to Computers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSI 1390: Introduction to Computers TA: Tapu Kumar GhoseOffice: STE 5014 Office hours: Thursday 1300 – 1400hrs. Email: tghos009@uottawa.ca

  2. 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)

  3. 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

  4. 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

  5. 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.

  6. 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.

  7. 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

  8. 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”); } }

  9. 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

  10. 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

  11. 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”); } }

  12. 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”); } } }

  13. 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; }

  14. 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.

  15. 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.

More Related