Strings Input & Output GEEN163 Introduction to Computer Programming “On two occasions I have been asked [by members of Parliament]: ‘Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?’ I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.” Charles Babbage MyCodeLab • Do 25 out of the 68 possible questions in sections 2.1 and 2.2 of MyCodeLab on the jblearning.turingscraft.com website • You will earn 4 points for each correct answer up to a maximum of 100 points • You can retry incorrect answers • Due by midnight on Tomorrow, Thursday Homework • The first programming assignment has been posted on Blackboard • It requires you to write four simple Java programs • Programs are due at midnight on Friday Overwriting Variables • Whenever a program assigns a value to a variable, it overwrite any previous value int wombat; wombat = 3; wombat = 5; // the value 3 has been lost Integer Division • Integers can only hold whole numbers • If division results in a fractional part, the fraction is dropped and the result is just the whole number. 8/2 is 4 4 / 3 is 1 8 / 3 is 2 -5 / 4 is -1 Operator Priority • When an expression has multiple operators the high priority operations are done first • When operators are of equal priority, they are executed left to right • High priority operators are: * / % • Lower priority operators are: + • Multiplication and division are done before addition and subtraction Evaluation of Expression dog = 3 + 4 * 5 * 2; dog = 3 + 20 * 2; dog = 3 + 40; dog = 43; Evaluation of Expression dog = (3 + 4 * 5) * 2; dog = (3 + 20) * 2; dog = 23 * 2; dog = 46; Clicking • Please be sure to register your clicker in Blackboard • There are now two GEEN163 sections in the Reef Polling app. Be sure you are connected to the correct section What will be displayed? int rat = 3, snake =5, spider = 7; spider = rat * (snake + 2); System.out.println(spider); A. B. C. D. 7 17 21 spider Math Functions • The Math class provides methods for most of the usual mathematical functions • The functions are written as Math.functionName( value ) • Note the period between Math and the function name Math Examples double fish = 9.0, bird = 3.14159265, frog; frog = Math.sqrt( fish ); // frog = 3.0 frog = Math.sin( bird / 6.0 ); // frog = 0.5 frog = Math.log( fish ) * bird; // frog = 6.903 Math Methods • There are many methods in the Math class • Some of the popular methods are: sin cos tan log log10 abs asin acos atan • The trigonometric functions take their arguments in radians, not degrees Math.pow • You can compute dog = catgoat dog = Math.pow( cat, goat ); • Note that all parameters to Math methods are doubles • The variable goat should be a double Attendance Policy • Students are required to attend all lectures, recitations and labs • The lab assignments are usually posted on Blackboard the night before • You must be present in Graham 203 to get credit for the lab What is Java for the equation? 𝑑𝑜𝑔 = A. B. C. D. 1 + tan(𝑐𝑎𝑡) dog = sqrt(1+tan(cat)); dog = Math.sqrt(1.0+Math.tan(cat)); dog = Math.tan(1.0+Math.sqrt(cat)); dog = Math.sqrt * 1.0 + Math.tan * cat; Math Constants • The Math class provides two constants that can be used in equations Math.PI 𝜋 ratio of circumference to diameter 3.141592653589793 Math.E e the base of the natural logarithms 2.718281828459045 Math Constant Examples 𝜋 𝑟𝑎𝑑𝑖𝑎𝑛𝑠 = 𝑑𝑒𝑔𝑟𝑒𝑒𝑠 ∗ 180 radians = degrees * Math.PI / 180.0; dog cow = cat + 𝑒 cow = dog / (cat + Math.E); Accuracy • Some numbers cannot be expressed accurately with a limited number of digits 1/3 = 0.3333333333333333333333333 etc. • Some values cannot be stored accurately in a binary computer • Depending on the order of operations, some results may differ in the last few digits • There are two special double values o NaN – Not A Number o Infinity Not A Number double cow = -25.0, goat; goat = Math.sqrt( cow ); System.out.println( "Square root of "+cow+" is "+ goat); This will display Square root of -25 is NaN String Variables • String is a Java class that can hold a bunch of characters • String are declared just like simple data types (i.e. int or double) using the type String String city; String county, state; String name = "Fred"; String Concatenation • The plus sign ( + ) can be used to concatenate strings together to make longer strings String cow = "dog", goat; goat = "bull" + cow; // goat = “bulldog” • You can concatenate String variables and String constants Mixing Strings and other Data • If you concatenate a String to a number data type, it will convert the number to a String and concatenate them String cow, goat; int myNum = 163; cow = "GEEN"; goat = cow + myNum;// goat is “GEEN163” • Any type will be converted to a String Operator Precedence • String concatenation has the same priority as addition String cow = "answer is " + 5*7; cow is answer is 35 Be Mindful of Operator Priority int cat = 3, dog = 4; String goat, frog; goat = "animals " + cat + dog; goat = "animals 3" + dog; goat = "animals 34"; frog = cat + dog + " animals"; frog = 7 + " animals"; frog = "7 animals"; What is the value of goat? String cow, goat; int myNum = 160; cow = "GEEN"; goat= cow + myNum + 3; A. B. C. D. cow163 GEEN163 GEEN1603 163 Java Output There are several ways to display output in Java JOptionPane.showMessageDialog JLabel in Java GUI Console output with System.out.println Displaying Data on the Screen • The System class can be used to display output on the console or the bottom window of jGRASP int rat = 5; System.out.println("Message "+rat); • This will display With or Without a New Line • The method System.out.print("text") displays a line, but does not go to a new line at the end System.out.println("first "); System.out.println("second "); System.out.print("third "); System.out.print("fourth "); Displays first second third fourth What is displayed? String cat = “meow”; String giraffe = “?”; String cow = “moo”; cow = cat + giraffe; System.out.println(cow); A. moo B. cow C. meow? D. meow+? Reading Input from the Keyboard • The java.util.Scanner class can be used to read values from the keyboard • At the beginning of your program, you have to create a Scanner object java.util.Scanner rabbit = new java.util.Scanner( System.in ); • I frequently use the variable name keyboard for a scanner object Reading an Int • After you have created a Scanner object, you can use the nextInt() method to read an int from the keyboard int hare; hare = rabbit.nextInt(); • The program will pause here and wait for the human to enter a number Reading a double • After you have created a Scanner object, you can use it to read a number with a decimal point using nextDouble() double bunny; bunny = rabbit.nextDouble(); • The program will wait for the human to enter a number Reading a String • You can read letters (up to a space) using next() or the whole line using nextLine() String cottontail; cottontail = rabbit.next(); cottontail = rabbit.nextLine(); Keeping Up • This class moves quickly. Avoid falling behind. • Tutors are available in Cherry 124 from 11:00am to 8:00pm • Tutor schedule in Blackboard course materials • Read the textbook • Copy and modify examples Tutoring Schedule 11:00 AM 12:00 PM 1:00 PM 2:00 PM 3:00 PM 4:00 PM 5:00 PM 6:00 PM 7:00 PM Monday Prince Wynn Kaleb Holley Kaleb Holley Ilo Wynn & Smith Jordyn Smith Patrick Grant Paul Biocco Paul Biocco Tuesday Prince Wynn Prince Wynn Kaleb Holley Kaleb Holley Jordyn Smith Jordyn Smith Holley Jupiter Paul Biocco Paul Biocco Wednesday Prince Wynn Hines & Holley Kaleb Holley Ilo Jordyn Smith Jordyn Smith Patrick Grant Paul Biocco Paul Biocco Thursday Prince Wynn Wynn & Gray Holley & Staggers Holley & Staggers Wynn & Smith Wynn & Smith Holley Jupiter Paul Biocco Paul Biocco Friday Prince Wynn Hines & Holley Kaleb Holley Gray Jordyn Smith Jordyn Smith Jordyn Smith Paul Biocco Paul Biocco Problem Solving • You have to know how to solve a problem before you can write a program to do it • You have to know what it is you want to solve before you can write a program Development and Execution • You write the Java program and make it work • Somebody else (we call them the “user”) runs the program • You, the programmer, do not know the data values the user will enter Organizing a Program • The general flow of a computer program is: –Read some input data –Calculate a value from the input –Display the results • Some programs will repeat this process many times Simple Program Structure public class MyProg { public static void main(String[] args) { data declarations input needed data calculate display results } } Don’t Forget the Comments • Write the comments as you write the program • Often writing an explanation of what the program is supposed to do helps to clarify the idea • When writing programs, I often write comments first identifying what the program should do at a certain point. Later I write the Java to make it do that. Comment Requirements • All programs must have a comment at the beginning that gives: – Your name – Short explanation of the program • The declaration of each variable must be accompanied by a comment explaining its logical value or purpose • Programs without these comments will lose points Input Values • What data will the program need to solve the problem? • Usually each input value will need a variable to hold the input • What data type is the input? int – if only whole numbers double – if decimal values are needed String – holds characters User Friendly • A good program will prompt the user for input • Tell the user what to enter System.out.println(“Enter the number of years”); Scanner Class • The java.util.Scanner class can be used to read numbers and strings • You must first create an object of the class Scanner java.util.Scanner keyboard = new java.util.Scanner(System.in); Scanner Methods • There are separate methods of the Scanner class to read different data types int cat = keyboard.nextInt(); double dog = keyboard.nextDouble(); String bird = keyboard.next(); Calculations • The calculations can range from trivial to extremely complex • Often you may need additional variables to hold the results • Sometimes you will need variables to hold temporary intermediate results Displaying Results • Display the results in an easy to understand format System.out.println("mass = " + aNumber); • You usually do not need additional variable for displaying the results Typing the Program • Most programs are not written from top to bottom • You may occasionally need to go back to the top and declare another variable MyCodeLab • Do 25 out of the 68 possible questions in sections 2.1 and 2.2 of MyCodeLab on the jblearning.turingscraft.com website • You will earn 4 points for each correct answer up to a maximum of 100 points • You can retry incorrect answers • Due by midnight on Tomorrow, Thursday Homework • The first programming assignment has been posted on Blackboard • It requires you to write four simple Java programs • Programs are due at midnight on Friday
© Copyright 2025 Paperzz