DOC PREVIEW
OSU CSE 1223 - Java Fundamentals

This preview shows page 1-2-3-21-22-23-42-43-44 out of 44 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 44 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSE 1223 Introduction to Computer Programming in Java Chapter 2 Java Fundamentals 1 Recall From Last Time Java Program import java util Scanner public class EggBasketEnhanced public static void main String args int numberOfBaskets eggsPerBasket totalEggs Scanner keyboard new Scanner System in System out print Enter the number of eggs in each basket eggsPerBasket keyboard nextInt System out print Enter the number of baskets numberOfBaskets keyboard nextInt totalEggs numberOfBaskets eggsPerBasket System out println eggsPerBasket eggs per basket System out println numberOfBaskets baskets System out println Total number of eggs is totalEggs 2 What Does EggBasketEnhanced Do Take a look at the program and see if you can figure out what it does There are some things in here we haven t talked about yet Can you guess what they re doing based on their names and how we re using them 3 What Is a Program Variable int numberOfBaskets eggsPerBasket totalEggs This is a declaration of three integer variables A variable is a named location to store data i e a container for data 4 What Is a Program Variable type int numberOfBaskets eggsPerBasket totalEggs variable names This is a declaration of three integer variables A variable is a named location to store data i e a container for data Each variable can hold only one type of data for example only integers only floating point real numbers or only characters All program variables must be declared before being used 5 What Is a Program Type A variable s type determines the kind of values that a variable can hold and what operations can be applied to it Java has two different types primitive types and reference types We ll discuss reference types later Some Java primitive types int integer whole values e g 0 1 13 231 double real values e g 0 0 3 1415 2 72 char single character values e g a 3 boolean only one of two values true false 6 How Do We Assign Change the Value of a Variable eggsPerBasket keyboard nextInt totalEggs numberOfBaskets eggsPerBasket Assignment statement variable expression Assigns the value of the expression on the right side of to the variable on the left side It does not mean equal like in math These expressions look different but they re both doing the same thing 7 What Is an Expression numberOfBaskets eggsPerBasket Program expressions are very much like arithmetic expressions you are familiar with usual operators parenthesis precedence rules etc Expressions can be evaluated to produce a value and they have a type the type of the value of the expression 8 What Is an Expression keyboard nextInt Expressions can also be the result of evaluating something known as a function The above is an example of this Program functions are very similar to mathematical functions Take inputs produce outputs from those inputs Such as y f x f x is a function where x is the input f x is the output More on this later 9 Numeric Operators Some common integer operators obvious obvious obvious integer division e g 6 2 3 5 2 2 19 5 mod operator i e remainder of integer division e g 6 2 0 5 2 1 19 5 Some common real operators real division Note that real division is approximate Sometimes the results you get will be surprising 10 Integer arithmetic For integers it is important to understand the and operators is integer division It is not exactly the same as division for real numbers 4 2 2 But 5 2 2 also Rule is divide then round closer to zero Not round down round toward zero So 5 2 2 11 Integer arithmetic For integers it is important to understand the and operators is integer remainder Works much like you might remember long division working when you were in grade school 5 2 1 4 2 0 Because 4 2 2 with 0 remaining 19 5 4 Because 5 2 2 with 1 remaining Because 19 5 3 with 4 remaining etc 12 Integer arithmetic For integers it is important to understand the and operators and work together x y y x y x 19 5 3 19 5 4 5 3 15 15 4 19 Use to check whether a number is even or not 10 2 0 11 2 1 13 Some Expressions What Are Their Values int i 12 j 5 k 3 double x 2 1 y 1 5 z 3 0 i j k 3 i j j i j x x y y x y z x y z 2 0 z x y 14 Output Statements System out println output1 output2 outputN System out print output1 output2 outputN Concatenates the various outputs quoted strings variables constants numbers and prints them to the screen println adds a newline What do the following statements output int day 19 System out print January day 2004 is Martin Luther King s Day 15 Input Statements Given the import import java util Scanner and the declaration Scanner in new Scanner System in Declare and input an integer value int i in nextInt Declare and input a real value double x in nextDouble Declare and input a whole line a string of characters String s in nextLine Input of a single character with the Scanner class is trickier we ll see it later 16 To Sum Up So far we have seen how to input values from the keyboard how to output messages values to the screen how to create variables to store values how to store values in variables and compute new values with expressions 17 It s Your Turn Now let s put it all together Write a Java program called ComputeArea which asks the user for the width and height of a rectangle and computes and prints the area of the rectangle 18 The char Type A variable of the char data type stores a single printable character A char constant or literal is a character in single quotes e g y For example char answer y System out println answer prints displays the letter y 19 The String Type A string is a sequence of characters The String type is used to declare variables that store strings The String type is not a primitive type it is known as a class or reference type A String constant is one or more characters in double quotes e g string constant Examples char charVariable a single quotes String stringVariable a double quotes String sentence Hello world 20 String Variables Declare a String variable String greeting Assign a value to the variable greeting Hello Use the variable as a String argument in a method call System out println greeting causes the string Hello to be displayed on the screen 21 Indexing Characters Within a String The index of a character within a string is an integer starting at 0 for the first character and gives the position of the character For example String str This is a string T h i s 0 1 2 3 4 i s 5 6 a 7 8 9 s t 10 11 r i n g 12 13 14 15 22 Methods …


View Full Document

OSU CSE 1223 - Java Fundamentals

Documents in this Course
Load more
Download Java Fundamentals
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 Java Fundamentals 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 Java Fundamentals 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?