1 / 11

Introduction to Programming

Introduction to Programming. Java Lab 6: if Statement. JavaLab6 lecture slides.ppt Ping Brennan ( Ping.Brennan@gmail.com ). 14 February 2014. Java Project. Project Name : JavaLab6. QuizGrading. LeapYear. Class QuizGrading. Reads in an integer (from the keyboard) as the score.

yachi
Download Presentation

Introduction to Programming

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. Introduction to Programming Java Lab 6: if Statement JavaLab6 lecture slides.ppt Ping Brennan (Ping.Brennan@gmail.com) 14 February 2014

  2. Java Project Project Name: JavaLab6 QuizGrading LeapYear

  3. Class QuizGrading • Reads in an integer (from the keyboard) as the score. • Displays the grade assigned to the score according to the following table: • Objectives • Understand the use of multiple if statements (if-elsestatements). • Applying relational operators: <, <=, >=

  4. Class QuizGrading (2) • Method • Applying • Read a numeric input (i.e. score) from the keyboard. • char grade = ' '; // Java type char • if (score >= 90) • { grade = 'A'; } • else if (score >= 80) • { grade = 'B'; } • else if/*To Do – write similar if–else statements to check the score • for grades ‘C’, ‘D’ and ‘E’. */

  5. Anatomy of Class QuizGrading import java.util.Scanner; public class QuizGrading { public static void main(String[] args) { /* To Do - write code to read score from the keyboard which is an integer of type int. */ char grade = ''; if (score >= 90) { grade = 'A'; } else if (score >= 80) { grade = 'B'; } else if // To Do - write similar Java if-else statements // Lastly, print the grade, along with an appropriate description } }

  6. Class LeapYear • Reads in a year and computes whether the year is a leap year. • Objectives • Understand the use of if-else statement • Applying the arithmetic operator %(computes the remainder of an integer division) • Using relational operators: ==, != • Using boolean operators: && , ||

  7. Class LeapYear (2) • Formulae • Applying • Read year input from keyboard using Scanner class • Dry-run of leap year algorithm if a year is divisible by 4 but not by 100, it is a leap year. if a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400. boolean a = (year % 4) == 0; // divisible by 4 boolean b = (year % 100) != 0; // not by 100 boolean c = ( (year % 100) == 0 ) // divisible by 100 && ( (year % 400) == 0 ); //and divisible by 400 Use the boolean expression:( a && ( b || c) )to find a leap year.

  8. Anatomy of Class LeapYear import java.util.Scanner; public class LeapYear { public static void main(String[] args) { /* To Do: (i) declare a variable year of type int (ii) read a numeric input (year) from the keyboard */ /* declare the boolean variables a, b and c as shown in slide 7 */ /* write an if-else statement and use the boolean expression shown in slide 7 */ /* write a println statement to display the result of the computation. */ } }

  9. Flow Chart for if-else statement[back to slide 4] Enter score: 95 start Enter score: 83 score >= 90 true false Enter score: 79 score >= 80 grade = ‘A’ true false Enter score: 60 grade = ‘B’ true score >= 70 false grade = ‘C’ more if-else statements Print statement

  10. Dry-run of LeapYear algorithm Back to Slide 7

  11. Dry-run of LeapYear algorithm (solution) Back to Slide 7

More Related