Presentation is loading. Please wait.

Presentation is loading. Please wait.

Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?

Similar presentations


Presentation on theme: "Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?"— Presentation transcript:

1 Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?

2 Vocabulary 1.Class – A blueprint for creating objects. 2.Object – Created from a class. Called an instance of a class. Every object has a state and a behavior. 3.state – a property; something an object knows about itself. (information stored in variables) 4.Behavior – Actions an object can perform (methods) 5.Variable – name for a memory location with a specific type. 6.new operator – used to create new objects 7.package – contains related classes under a common name. java.util java.awt 8.Scanner - class that provides methods for reading input of various types from various sources. 9.Parameter – used to supply extra information to methods. All methods must have a parameter even if it is empty. 10.Argument – The information supplied in the parameter. 11.Encapsulation – information hiding 12.Dot Operator - objects call methods with a dot operator

3 Variables A variable is an identifier. A name for a memory location to store specific data. It can hold a primitive data type – boolean, double, int, char It can represent the name of an object and hold a reference to a that object in memory.

4 What is an Class? Everything in Java is in a class Java has predefined classes in library called the Java API – Application Programming Interface. The library is made up of packages that contains classes that have things in common. Package contains like classes. (Java API) Classes create objects Objects have a state and a behavior ObjectState (variable info) Behavior (action) Dogheight, weight, breed, color, age bark, eat, move, play, sleep

5 Java class libraries, import Java class libraries: Classes included with Java's JDK. – organized into groups named packages – To use a package, put an import declaration in your program. Syntax: // put this at the very top of your program import packageName.*; Scanner is in a package named java.util import java.util.*; import java.util.Scanner;

6 Import statement need to use Scanner class How to create Scanner object

7 Creating Objects To create any object in java use the word new ClassName objectName = new ClassName( ); The Scanner needs to know how to get information into the computer. Use System.in Scanner name = new Scanner(System.in); – Example: Scanner console = new Scanner(System.in); Class object new Class (parameter) Called a parameter. Used to supply extra information Needed for classes, methods

8 Scanner methods Scanner console = new Scanner(System.in); System.out.print("How old are you? "); int age = console.nextInt(); System.out.println("You'll be 40 in " + (40 - age) + " years."); prompt: A message telling the user what input to type. MethodDescription nextInt() reads a token of user input as an int nextDouble() reads a token of user input as a double next() reads a token of user input as a String nextLine() reads a line of user input as a String

9 Objects call methods Class creates an object. Object calls the method from the class with a dot operator. Scanner scan = new Scanner(System.in); int num; double num2; System.out.println(“Enter a number”); num = scan.nextInt(); System.out.println(“Enter a double number”); num2 = scan.nextDouble();

10 Scanner program import java.util.Scanner; // import statement needs to use the Scanner public class ScannerProgram { public static void main(String[]args) { int age; String name; double gpa; Scanner console = new Scanner(System.in); System.out.println("How old are you? "); age = console.nextInt(); System.out.println("What is your name?"); name = console.next(); System.out.println("What is your gpa"); gpa = console.nextDouble(); System.out.println("You'll be 40 in " + (40 - age) + " years."); System.out.println("Your name is " + name); System.out.println("Your gpa is " + gpa); }

11 Another Scanner example import java.util.*; // so that I can use Scanner public class ScannerSum { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Please type three numbers: "); int num1 = console.nextInt(); int num2 = console.nextInt(); int num3 = console.nextInt(); int sum = num1 + num2 + num3; System.out.println("The sum is " + sum); } Output (user input underlined): Please type three numbers: The sum is 27 – The Scanner can read multiple values from one line.

12 Assignment Change the programs you created to use the Scanner. Rename as follows: AverageScanner PenniesScanner SumDiffProdScanner McDonaldsScanner Go to schoology.com. Create an account You are a student. Use the access code MF4SB-S4Z84 You will Click on courses and Choose AP Computer Science Click on assignments. Upload the programs listed.


Download ppt "Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?"

Similar presentations


Ads by Google