Computer Programming I
Computer Programming I CS 121
Popular in Course
Popular in ComputerScienence
This 2 page Class Notes was uploaded by Kylee VonRueden on Wednesday September 30, 2015. The Class Notes belongs to CS 121 at Pace University - New York taught by Staff in Fall. Since its upload, it has received 14 views. For similar materials see /class/217106/cs-121-pace-university-new-york in ComputerScienence at Pace University - New York.
Reviews for Computer Programming I
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 09/30/15
CS 1 21IS223 Reading from the keyboard with Scanner Printing out information is ne but we also need to be able to read it in There are a number of places that we can read from including the keyboard les and databases We will start with the keyboard Everything typed at the keyboard goes into a location in the computer called a buffer When you hit the enter key the data in the buffer is made available to the program The program starts at the beginning of the line and reads what it is asked for The reading is done by a class called Scanner new in Java 5 Strings are read using neXtLine integers using neXtInt 0 and doubles using neXtDouble 0 When reading from the keyboard we always need to have a prompt preceding the reading so that the user knows what is eXpected If you type in a letter when the program is eXpecting an int you will get a runtime error Scanner is in the javautil package so it has to be imported into the program We will see other import statements in the future package zoo import javautilScanner public class Panda private String name private int age Scanner scan new Scanner Systemin The Scanner class has to be instantiated public void getData Systemoutp1int quotEnter the panda s name quot Prompt for the user name scanneXtLine Systemoutp1int quotEnter the panda s age quot Prompt for the user age scanneXtInt 0 getData public void displayData Systemoutp1intln name quot s age is quot age displayData Panda We also have to change the ZooKeeper class in order to use call the new method in the Panda class package zoo The zoo keeper manages the data for the animals in the zoo public class ZooKeeper public static void mainSt1ing args Panda panda new Panda 0 pandagetData pandadisplayData main ZooKeeper Doing arithmetic with Java and other languages Java has the usual arithmetic operators The rst three work the expected way but the divide does not If the numbers are integers it performs the division and then drops the fractional part It does not round If the numbers are doubles it divides normally and keeps the fractional part with a lot of decimal places The fractional part can be obtained using the operator 17 3 is 2 Parentheses change the order that operations are done in the usual way Examples 1 234is2714 2 234is6410 3 15 4 3 since the fractional part is dropped 4 4 15 0 since the quotient is zero 5 40 15 0266666666666666 the 40 also affects the 15 6 2746while2743 We can change from one type to another using a cast The new type is indicated by the name of the data type in parentheses precedes the number or variable 1 int 27 2 2 double 5 50 3 int 155 6 90 4 double 25 3 833333333333334
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'