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

Slides:



Advertisements
Similar presentations
Review pages for the AP CS Exam
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Introduction to Programming
Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Programming Methodology (1). Implementing Methods main.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Importing methods in the Java library. Previously discussed Method = a collection of statements that performs a complex (useful) task A method is identified.
Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.
Building Java Programs
Computer Programming Lab 8.
Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-3: Interactive Programs w/
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
***** SWTJC STEM ***** Chapter 3-1 cg 36 Java Class Libraries & API’s A class library is a set of classes that supports the development of programs. Java.
1 CSE 142 Lecture Notes Interactive Programs with Scanner Chapter 4 Suggested reading: Suggested self-checks: Section 4.10 #1-4 These lecture.
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
Topic 11 Scanner object, conditional execution Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Objects and Classes; Strings. 2 Classes and objects class: A program entity that represents either 1.A program / module, or 2.A type of objects* –A class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
Interactive Programs with Scanner. 2 Input and System.in interactive program: Reads input from the console. –While the program runs, it asks the user.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Chapter 2 Console Input and Output Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Building Java Programs
TemperatureConversion
CSCI 161 – Introduction to Programming I William Killian
Yanal Alahmad Java Workshop Yanal Alahmad
Building Java Programs
Java Programming: From Problem Analysis to Program Design, 4e
Topic 11 Scanner object, conditional execution
INPUT STATEMENTS GC 201.
Introduction to Classes and Methods
Chapter 2: Basic Elements of Java
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 – 3.4, 4.1, 4.5
Building Java Programs
Building Java Programs
Optional Topic: User Input with Scanner
Presentation transcript:

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

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

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.

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

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;

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

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

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

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();

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); }

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.

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.