Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –

Slides:



Advertisements
Similar presentations
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Advertisements

CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
LAB 10.
Computer Programming Lab(4).
COMP More About Classes Yi Hong May 22, 2015.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Introduction to Object-Oriented Programming
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
GCOC – A.P. Computer Science A. College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose,
EXAM 1 REVIEW. days until the AP Computer Science test.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Classes - Intermediate
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Dale Roberts Introduction to Java - Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
1 Simple Input Interactive Input - The Class Scanner n Command-Line Arguments.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
CPSC 233 Tutorial Xin Liu Feb 14, Tips on keyboard input How to detect that the user just hit enter when prompted for a string import java.util.Scanner;
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Introduction to programming in java
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Input, Output and Variables GCSE Computer Science – Python.
The need for Programming Languages
3 Introduction to Classes and Objects.
Yanal Alahmad Java Workshop Yanal Alahmad
2.5 Another Java Application: Adding Integers
Computer Programming Methodology Input and While Loop
TK1114 Computer Programming
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
Introduction to javadoc
While Statement.
CSC 113 Tutorial QUIZ I.
An Introduction to Java – Part I, language basics
Introduction to Classes and Methods
A+ Computer Science INPUT.
Group Status Project Status.
Recap Week 2 and 3.
Chapter 6 Methods.
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Week 4 Lecture-2 Chapter 6 (Methods).
Building Java Programs
Methods/Functions.
Computer Science Club 1st November 2019.
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() – double d = scanner.nextDouble(); – String s = scanner.nextLine(); Differences from IO.read methods – There is no popup window – You just enter by typing at the bottom – You need to prompt the user what to enter

Command line Input Using command line from Jgrasp – Click on build, then on run arguments – Enter the arguments that you want separated by spaces – Run the program and the program will use those arguments Using the real command line – In Windows, click on start, then on run, then type cmd and click OK. You will be able to type Windows commands – Type: java – Your program will run and use those command line arguments

Command Line Example 1.In Jgrasp, click on build, and then on run arguments, and enter into the text box at the top 2.Enter the following program public class CommandLine { public static void main(String[] args) { int sum = 0; for (int i=0; i<args.length; i++) { sum += Integer.parseInt(args[i]); } System.out.println("Sum = " + sum); } } 3.Compile and run, the output should be: sum = 15 Note: parseInt is a static method that converts a string to an integer

Multiple Classes 1.Some applications can have hundreds of classes that work together to solve a problem 2.Each class is entered as a separate.java source file. 3.Compiling the main program will also compile all the classes it needs. Design Challenge: How do we define an appropriate number of classes to create a structure for an application. This is a skill you learn in other Computer Science classes Example: Creating a phone book (Following slides) – Class for a person – Class for a list of persons – Main class So far our applications had only a single class

The Person class public class Person {private String name, number; // Instance Variables public Person(String name, String number) // Constructor {this.name = name; this.number = number; } public String toString() // Return nicely formatted string {String spaces = “ ”; String s = (name + spaces).substring(0,spaces.length()); String t = (number + spaces).substring(0,spaces.length()); return s + “ ” + t; }// End of toString method }// End of Person class Note: We don’t want outside class methods accessing the instance variables, hence private Note: The toString() method is handy as you will see Note: The strings s and t mean that all Person outputs will be formatted the same way

Class for list of Persons public class PersonList {private Person[] persons; private int howMany; public PersonList(int size) { persons=new Person[size]; howMany=0; } public boolean addPerson(String person, String number) {if (howMany == persons.length) return false; else persons[howMany++] = new Person(person, number); return true; } public String toString() { String str = “Phone Book Listing\n”; for (int i=0; i<howMany; i++) str += persons[i].toString() + “\n”; return str;}

Main class public class Main {private static PersonList personList; public static void main(String[] args) {String phone, name; Scanner in = new Scanner(System.in); personList = new PersonList(10); do { System.out.print(“Enter name: ” ); name = in.nextString(); System.out.print(“Enter number: ”); phone = in.nextString(); } while (personList.addPerson(name, phone)); System.out.println(personList); } Input a group of persons, and then print out the list

Final Thoughts Using multiple classes makes the application more general – The Person class can be used by other programs – The details of the Person class is internal. Users only have to know how to call its methods – We did this from day one without knowing it Examples: System.out.println(), Math.round(), and IO.readString() The private modifier is important – Normally make instance variables visible only where they are declared – This makes for easier modifications to an application – It enables the principle of limiting scope The purpose of a constructor is to initialize an object General Rule: Use the static modifier only when necessary