1 / 12

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.

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

  3. Solutions • We will be using the method called readInt() of the Keyboard.java • In order to do so, we have to compile the Keyboard.java program • Save the Keyboard.java program in the bin 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. Source code import java.io.*; class Triangle { public static void main(String[] args){ int x, y, z; Keyboard KB = new Keyboard(); System.out.print("Enter the largest side of the triangle: "); x = KB.readInt(); System.out.print("Enter the second side of the triangle: "); y = KB.readInt(); System.out.print("Enter the third side of the triangle: "); z = KB.readInt(); if((x < y) || (x < z)){ // checking for largest side System.out.println(“Enter the largest side: ”); x = KB.readInt(); } if((x * x) == ((y * y) + (z * z)) System.out.print(“This is a right triangle”); else System.out.print(“Not a right triangle”); } }

  5. Loops • Sometimes we need to repeat a set of statements • For example: find the sum of all the digits • The program would look like as follows: int sum = 0; sum = sum + 1; // sum = 0 + 1 = 1 sum = sum + 2; // sum = 1 + 2 = 3 sum = sum + 3; // sum = 3 + 3 = 6 ……… sum = sum + 9; // sum = 36 + 9 = 45 System.out.println(sum);

  6. Loops • Instead of writing similar set of statement, we use loop • Three types of loop: • for • while • do-while

  7. for-loop • The structure of for loop: for (initialization ; test_condition ; increment){ // body of the loop } • the repeated statements are placed in the body of the loop • initialization : initialize the counter of the loop • test_condition: tests whether the counter is within the limit • Increment: modifies the counter

  8. for-loop • The program for finding the sum of all the digits using for-loop would look like as follows: class sum{ public static void main (String[] args){ int sum = 0; for (int i = 0; i < 10; i = i + 1){ sum = sum + i; } System.out.println(sum); } }

  9. Some useful statements • x = x + 1 means we are incrementing the value of x by 1. • x = x + 1 can be written as x++ • Similarly, x = x -1 can be written as x— • x = x + 5 can be written as x += 5 • x = x – 10 can be written as x -= 10 • x = x * 7 can be written as x *= 7 • x = x / 9 can be written as x /= 9

  10. while-loop • The structure of while loop: while (test_condition){ // body of the loop } • the repeated statements are placed in the body of the loop • test_condition: tests whether the counter is within the limit • Modification of counter is done inside the body • Usually, while loop is used when we have NO idea how many times the loop body will be executed. • for loop is usually used when we know the number of iteration of the loop body.

  11. while-loop • Example: Take integers from user and keep adding them until any negative number is entered. Class addition{ public static void main (String[] args){ int x, sum; x = sum = 0; Keyboard k = new Keyboard(); while (x >= 0){ x = k.readInt(); sum += x; } System.out.println(sum); } }

  12. Practice • Find the sum of the squares of all the even numbers between 95. • using a while statement, produce the sum 1+2+3+ ...+n, where a value for n is entered by the user.

More Related