AP Computer Science Static Methods, Strings, User Input



Similar documents
CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Sample CSE8A midterm Multiple Choice (circle one)

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Introduction to Programming

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Basics of Java Programming Input and the Scanner class

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Introduction to Java

Introduction to Java. CS 3: Computer Programming in Java

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

Chapter 2: Elements of Java

J a v a Quiz (Unit 3, Test 0 Practice)

JAVA ARRAY EXAMPLE PDF

Interactive Applications (CLI) and Math

Install Java Development Kit (JDK) 1.8

Data Structures Lecture 1

Chapter 2 Elementary Programming

Using Files as Input/Output in Java 5.0 Applications

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

CS 106 Introduction to Computer Science I

AP Computer Science Java Mr. Clausen Program 9A, 9B

Chapter 3. Input and output. 3.1 The System class

Comp 248 Introduction to Programming

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

More on Objects and Classes

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

AP Computer Science Java Subset

D06 PROGRAMMING with JAVA

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Building Java Programs

Carron Shankland. Content. String manipula3on in Java The use of files in Java The use of the command line arguments References:

Building Java Programs

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

D06 PROGRAMMING with JAVA

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Chapter 2 Introduction to Java programming

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

CS 106 Introduction to Computer Science I

Topic 11 Scanner object, conditional execution

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Programmierpraktikum

Object-Oriented Programming in Java

LOOPS CHAPTER CHAPTER GOALS

LAB4 Making Classes and Objects

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Java CPD (I) Frans Coenen Department of Computer Science

Introduction to Object-Oriented Programming

Programming Fundamentals I CS 110, Central Washington University. November 2015

Part I. Multiple Choice Questions (2 points each):

Building Java Programs

Building Java Programs

Lab 5: Bank Account. Defining objects & classes

BANK B-I-N-G-O. service charge. credit card. pen. line nickel interest cash bank. ATM dollar check signature. debit card.

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Iteration CHAPTER 6. Topic Summary

For live Java EE training, please see training courses

Basic Programming and PC Skills: Basic Programming and PC Skills:

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

1st Grade Math Standard I Rubric. Number Sense. Score 4 Students show proficiency with numbers beyond 100.

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

How to use the Eclipse IDE for Java Application Development

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

The following program is aiming to extract from a simple text file an analysis of the content such as:

6.1. Example: A Tip Calculator 6-1

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Java Basics: Data Types, Variables, and Loops

Security Module: SQL Injection

Some Scanner Class Methods

1001ICT Introduction To Programming Lecture Notes

Programming in Java Course Technology, a part of Cengage Learning.

Homework/Program #5 Solutions

1.1 Your First Program

CSE 8B Midterm Fall 2015

Line-based file processing

Primitive data types in Java

Java Crash Course Part I

Basic Java Syntax. Slides 2016 Marty Hall,

In this Chapter you ll learn:

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

CS 112/ Section 02 Hilal Ünal Aslıhan Ekim Merve Özkılınç Notes of March 11, 2008 and March 13, 2008: Pig Latin: add adday. main.

Outline Basic concepts of Python language

Computer Programming I

Lecture 1 Introduction to Java

13 File Output and Input

Chapter 2 Basics of Scanning and Conventional Programming in Java

Variables, Constants, and Data Types

Transcription:

AP Computer Science Static Methods, Strings, User Input Static Methods The Math class contains a special type of methods, called static methods. A static method DOES NOT operate on an object. This is because the methods of the Math class operate on numbers, and in Java numbers are NOT objects. Instead, Math methods require a parameter. How can you tell a method, from a class, from an object? CLASS - names should ALWAYS start with a capital letter (e.g., Math, Rectangle, String) METHODS names should NEVER start with a capital letter so they are not confused with Class names (you should use capitals in the middle of the name to improve understanding, e.g., getbalance()). Method calls are ALWAYS followed by () sometimes with parameters, sometimes empty (). OBJECTS names should NEVER start with a capital letter so they are not confused with Class names. The name of an object should have similarity with the name of the Class that creates it (e.g., a object of the Rectangle class might be called myrectangle.) Objects can be distinguished from methods because they are not followed by (). EXAMPLES: Math.sqrt(4); System.out.println( Hello ); myrectangle.getheight();

How to program a mathematical formula (see HOW TO 4.1 for detailed answer). 1. Understand the problem what are the inputs? outputs? Example: Stamp Machine 2. WORK OUT EXAMPLES BY HAND. (If you can t do it, you won t be able to make the computer do it!) Customer enters $5.00 in a stamp machine. How many $.44 stamps will they receive and how many $.01 stamps. 3. Turn the arithmetic steps into a mathematical equation that represents the situation. 4. Turn the mathematical equations into Java expressions.

5. Build a class that carries out the calculations. 6. Test the class (use your examples from step 2 plus some others; be sure you know the answer first, so you know whether your program works!) String Methods A string is a sequence of characters. Specific strings are OBJECTS of the STRING class. You can concatenate strings using.concat() method or the + sign. - Only 1 side of the + sign needs to be a string; the other one will be forced into being a string. String name = Agent int n = 7; System.out.println(name + 7);.length() is a method of the String class that will return the # of characters in the string. Integer.parseInt() and Double.parseDouble() will turn a character that is a number into the value of the number. This is usually used to interpret user input. (these are both static methods.)

.substring(start, onepastend) will return the characters of a string as specified by the parameters (characters in a string start at position 0.) (If second parameter is missing, the substring will go to the end of the string.) Reading User Input The Scanner class is used for capturing user input from a console window. To use this class, include: import java.util.scanner at the beginning of your program. You can create a Scanner object as follows: Scanner in = new Scanner(System.in); Methods you can use include: in.nextint() in.nextdouble() in.nextline() in.next()

Example of getting user input for CashRegister Class ch04/cashregister/cashregistersimulator.java 1 import java.util.scanner; 2 3 /** 4 This program simulates a transaction in which a user pays for an item 5 and receives change. 6 */ 7 public class CashRegisterSimulator 8 { 9 public static void main(string[] args) 10 { 11 Scanner in = new Scanner(System.in); 12 13 CashRegister register = new CashRegister(); 14 15 System.out.print( Enter price: ); 16 double price = in.nextdouble(); 17 register.recordpurchase(price); 18 19 System.out.print( Enter dollars: ); 20 int dollars = in.nextint(); 21 System.out.print( Enter quarters: ); 22 int quarters = in.nextint(); 23 System.out.print( Enter dimes: ); 24 int dimes = in.nextint(); 25 System.out.print( Enter nickels: ); 26 int nickels = in.nextint(); 27 System.out.print( Enter pennies: ); 28 int pennies = in.nextint(); 29 register.enterpayment(dollars, quarters, dimes, nickels, pennies); 30 31 System.out.print( Your change: ); 32 System.out.println(register.giveChange()); 33 } 34 } Output Enter price: 7.55 Enter dollars: 10 Enter quarters: 2 Enter dimes: 1 Enter nickels: 0 Enter pennies: 0 Your change: 3.05 ADVANCED TOPIC To learn how to format your output, see Advanced Topic 4.6