Know for Quiz Everything through Last Week and Lab 7

Slides:



Advertisements
Similar presentations
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.
Advertisements

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Objects, Methods, Parameters, Input Lecture 5, Thu Jan
1 CS 105 Lecture 8 Strings; Input Failure Mon, Mar 7, 2011, 3:39 pm.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
COMP 110 Branching Statements and Boolean Expressions Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to Java Programming Language May 2015 Kyung Eun Park COSC Introduction to Computer Science II.
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
Announcements Quiz 2 Grades Posted on blackboard.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
EXAM 1 REVIEW. days until the AP Computer Science test.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
The String Class A String is an object. An object is defined by a class. In general, we instantiate a class like this: String myString = new String(“Crazy.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
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 1.5 The New Java Mike Orsega Central Carolina CC.
CSC1401 Strings (text). Learning Goals Working with Strings as a data type (a class) Input and output of Strings String operations.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Catie Welsh February 2,  Program 1 Due Today by 11:59pm today  Program 2 Assigned Today  Lab 2 Due Friday by 1:00pm 2.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
CSCI 51 Introduction to Computer Science Joshua Stough February 3, 2009.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
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.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
(Dreaded) Quiz 2 Next Monday.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Key Words / Reserved Words
Chapter 2 Clarifications
Introduction to programming in java
CS 160 Final Review.
Crash course in the Java Programming Language
Elementary Programming
Chapter 6 More Conditionals and Loops
File Input and Output TOPICS File Input Exception Handling File Output.
Chapter 5: Control Structures II
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
SELECTION STATEMENTS (1)
Quiz Next Monday.
File Input and Output TOPICS File Input Exception Handling File Output.
BIT115: Introduction to Programming
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
Chapter 6 More Conditionals and Loops
Classes Variables That Are Not of a Built-in Type Are Objects
Selection (if-then-else)
An Introduction to Java – Part I, language basics
Introduction to Classes and Methods
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade
File Review Declare the File Stream Object Name
CSC1401 Input and Output (with Files)
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Repetition Statements
(Dreaded) Quiz 2 Next Monday.
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
A type is a collection of values
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

Know for Quiz Everything through Last Week and Lab 7 Loops: while, for, do-while switch-case Statement

Strings Revisited String Is a Type (Object) in JAVA Automatically imported in java.lang May Initialize or Assign with Double Quoted Values Can Add String to String with ‘+’ Operator CANNOT Compare using Equality Operators (uses .equals() method instead) Methods Available to Access single characters Get length of String Compare Strings

Objects Objects: Types of Variables Made from Built-In Types Variables Can be Declared as an Object Type Objects Usually Have Functions Tied to Them Member functions Public functions Interface functions Methods String Is an Example of an Object Type StringVariable.length() Returns the Number of Characters in the String

Methods Also called Procedures, Functions Have Parenthesis (e.g., method() ) May be Passed Arguments, or Parameters Parameters are values to initialize new variables in method Parameters used to pass information to method

String Methods charAt(int index): return char at named index (starting at 0) equals(String str): return boolean length(): return int length of String

String myString = "My Dog Sandy",compareString; System.out.println("The fourth character is: " + myString.charAt(3)); if (myString.equals("my dog sandy")) System.out.println("Strings are equal"); else System.out.println("Strings are not equal"); compareString = myString + myString; System.out.println("Added strings are: " + compareString); Output: The fourth character is: D Strings are not equal Added strings are: My Dog SandyMy Dog Sandy

System.out.println("Length of string is: " + myString.length()); Output: Length of string is: 12

import java.util.Scanner; public class MyProgram { public static void main( String [ ] args) Scanner scan = new Scanner(System.in); String lastname; boolean instructor = false; System.out.print(“Enter last name:”); lastname = scan.next(); if(lastname.equals(“hanrath”)) instructor = true; // privileged else instructor = false; // not privileged }

More on Input Input Failure Common *MUST* Be Part of Test Plan Pages 310-313 in Anderson Check Input for Type Desired hasNextInt(), hasNextDouble() If not, Notify User, and Get Rid of Line Once hasNotXXX() returns true, safe to read in value

Input Failure Example import java.util.*; class InputFailureExample { public static void main(String[] args) int numE; String garbage; Scanner scan = new Scanner(System.in); System.out.print("Enter Integer: "); while (!scan.hasNextInt()) garbage = scan.nextLine(); System.out.println("Please try again."); } numE = scan.nextInt(); System.out.println("You entered: " + numE);

Arrays Syntax: type variableName[] = new type [size]; Memory Is Set Aside for size Items of type Each Variable Location in Array Is Accessed by Offsetting into the Array with an Integer Expression Legitimate Offsets Are 0 to size-1

Array Example int [] values = new int[15]; values[0] = 150; System.out.println( values[0]); values[3] = values[0] + 6;

Array Example final int ARRAY_SIZE = 100; int offset; int [] numArray = new int [ARRAY_SIZE]; for(offset = 0; offset < ARRAY_SIZE; offset++) { numArray[offset] = 0; } for(offset = 0; offset < numArray.length; offset++) numArray[offset] = offset;

Files Data in Main Memory is “volatile” File: Place for “permanent” data storage C: drive, A: drive, Flash drive, etc. Main Memory File main() int num; string firstname; Disk

Output File Streams import java.io.*; class FileOutput { public static void main(String [ ] args) throws IOException int i; FileOutputStream ofile = new FileOutputStream("data2.txt",false); //true:APP PrintWriter pw = new PrintWriter(ofile); for (i=0;i < 100; i = i + 2) pw.println(i); } pw.close(); // Writes data to file on disk

Input Files import java.io.*; import java.util.Scanner; class FileInput { public static void main(String [ ] args) throws IOException File ifile = new File("data2.txt"); Scanner scan = new Scanner(ifile); while (scan.hasNextInt()) i = scan.nextInt(); }

mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary import java.util.Scanner; import java.io.*; class FormatFileData { public static void main(String [ ] args) throws IOException int loops, integer, i; float decimal; String name; File ifile = new File("mydata.txt"); Scanner scan = new Scanner(ifile); loops = scan.nextInt(); for(i= 0 ; i < loops; i++) integer = scan.nextInt(); decimal = scan.nextFloat(); name= scan.next(); System.out.print(integer + " "); System.out.print(decimal + " "); System.out.print(name + " "); System.out.println(); } mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary -23 -4.55 Smith -3 -4e3 xyz Output: 8 9.3 Jon 6 14.335 Bill 0 3.567E10 Mary -23 -4.55 Smith -3 –4000.0 xyz

Know for Quiz 2 Everything through Last Week and Lab 7 Loops: while, for, do-while switch-case Statement