Unformatted text preview:

Introduction to Algorithms and Data Structures Lecture 1 A Brief Review of Java Basic Framework of a Java Program ClassName Introductory comment Public class public static void main String args variable declarations statements The Average of Three Values public class AverageThree public static void main String args int average 2 4 6 3 System out println The average of 2 4 and 6 average Example Asking the user for name age Available input name and age Required output a message asking the user if s he is x years old Initial algorithm 1 Ask the user for name and age 2 Print message Refining the algorithm for asking name and age 1 Ask the user for name and age 2 Print message 1 1 Ask the user for name 1 2 Ask the user for age 1 1 Ask the user for name 1 2 Ask the user for age 2 Print message 1 1 1 Prompt the user for name 1 1 2 Read the name 1 2 1 Prompt the user for age 1 2 2 Read the age 1 1 1 Prompt the user for name 1 1 2 Read the name 1 2 1 Prompt the user for age 1 2 2 Read the age 2 Print message System out println What s your name t name keyb next System out println How old are you t age keyb nextInt System out println What s your name t name keyb next System out println How old are you t age keyb nextInt 2 Print message System out println Are you really age name Age java import java util Scanner public class Age Ask the user for his her name and age public static void main String args Scanner keyb new Scanner System in String name new String int age Get the name System out println What s your name t name keyb next Get the age System out println How old are you t age keyb nextInt Print the message System out println Are you really age name Improving our program We ll change the program to find out which of two people is older Available input names and ages for both people Required output a message stating who is older Initial algorithm 1 2 3 Ask the user for name and age of person 1 Ask the user for name and age of person 2 Print message 1 Ask the user for name and age of person 1 2 Ask the user for name and age of person 2 3 Print message System out println What s your name t name1 keyb next System out println How old are you t age1 keyb nextInt System out println What s your name t name2 keyb next System out println How old are you t age2 keyb nextInt System out println What s your name t name1 keyb next System out println How old are you t age1 keyb nextInt System out println What s your name t name2 keyb next System out println How old are you t age2 keyb nextInt 3 Print message 3 if person1 is older 3 1 print person 1 is older 3 2 else if person 2 is olderr 3 2 1 print person2 is older 3 3 else print they re the same age System out println What s your name t name2 keyb next System out println How old are you t age2 keyb nextInt 3 if person1 is older 3 1 print person 1 is older 3 2 else if person 2 is older 3 2 1 print person2 is older 3 3 else print they re the same age 3 if person1 is older 3 1 print person 1 is older 3 2 else if person 2 is olderr 3 2 1 print person2 is older 3 3 else print they re the same age System out println How old are you t inputLine keyboard readLine age1 Integer parseInt inputLine 3 if person1 is older 3 1 print person 1 is older 3 2 else print person2 is older if age1 age2 System out println name1 is older than name2 else if age2 age1 System out println name2 is older than name1 else System out println name1 and name2 are the same age Age2 java import java util Scanner public class Age2 public static void main String args Scanner keyb new Scanner System in String name1 new String name2 new String int age1 age2 Get the name and age for person 1 System out println What s your name t name1 keyb next System out println How old are you t age1 keyb nextInt Get the name and age for person 2 System out println What s your name t name2 keyb next System out println How old are you t age2 keyb nextInt Print the message if age1 age2 System out println name1 is older than name2 else if age2 age1 System out println name2 is older than name1 else System out println name1 and name2 are the same age System out println What s your name t name1 keyboard readLine System out println How old are you t inputLine keyboard readLine age1 Integer parseInt inputLine System out println What s your name t name2 keyboard readLine System out println How old are you t inputLine keyboard readLine age2 Integer parseInt inputLine if age1 age2 System out println name1 is older than name2 else if age2 age1 System out println name2 is older than name1 else System out println name1 and name2 are the same age Iteration There are three different ways of writing loops in C while loop do while loop for loop while loop while loops are generally conditional loops and the syntax is while condition statement or while condition statement s do while loops loops have their test at the beginning of the loop they might not even be executed once while do while loops have their test at the end so they are guaranteed to be executed at least once The syntax is do statement s while condition Age3 java import java util Scanner public class Age3 final int namelen 25 main Print name and age of user public static void main String args Scanner keyb new Scanner System in String name new String inputLine new String char answer int age Get the inputs System out println What s your name t name keyb next System out println How old are you t age keyb nextInt Respond to the user do System out println Are you really age name inputLine keyb next answer inputLine charAt 0 while answer y answer n if answer n System out println I didn t think so for loops loops are used for counting loops although they are really shorthand for while loops for The syntax is for expression1 expression2 expression3 statement or for expression1 statement s expression2 expression3 for and while loops for loops can be rewritten using while loops e g for i 0 i 8 i System out println i is equivalent to i 0 while i 8 System out println i i celsius1 java public class Celsius celsius Convertinb from celsius to fahrenheit …


View Full Document

ADELPHI CS 172 - A Brief Review of Java

Documents in this Course
Load more
Download A Brief Review of Java
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view A Brief Review of Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view A Brief Review of Java and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?