Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

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.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
Announcements Quiz 2 Grades Posted on blackboard.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Classes, Objects, and Methods
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
One Dimensional Array. Introduction to Arrays Primitive variables are designed to hold only one value at a time. Arrays allow us to create a collection.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 1, page 1 Sun Certified Java 1.4 Programmer Chapter 1 Notes Gary Lance
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.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
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.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Announcements Final Exam:TBD. public static void main(String [] args) { final int ASIZE = 5; int [] intArray= new int[ASIZE]; for(int i = 0; i < ASIZE;
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Classes - Intermediate
Lecture 10: Object Oriented Programming Tami Meredith.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
(Dreaded) Quiz 2 Next Monday.
Chapter 7 User-Defined Methods.
3 Introduction to Classes and Objects.
Yanal Alahmad Java Workshop Yanal Alahmad
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Chapter 3: Using Methods, Classes, and Objects
Selenium WebDriver Web Test Tool Training
Chapter 5: Control Structures II
Quiz Next Monday.
CS 302 Week 11 Jim Williams, PhD.
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Classes Variables That Are Not of a Built-in Type Are Objects
Classes, Objects, and Methods
An Introduction to Java – Part I, language basics
Know for Quiz Everything through Last Week and Lab 7
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade
File Review Declare the File Stream Object Name
Arrays Syntax: type variableName[size];
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.
Review for Midterm 3.
CS 240 – Advanced Programming Concepts
Presentation transcript:

Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())

Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append (boolean) –PrintWriter variable initialized to FileOutputStream variable For input from file: –File variable initialized to filename (String) –Scanner variable initialized to File variable

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 Jon Bill e9 Mary Smith -3 -4e3 xyz Jon Bill E10 Mary Smith -3 – xyz Output:

Methods public static void main(String [] args) { … displayVals(); … displayVals(); … displayVals(); return(0); } public static void displayVals() { System.out.println(…); return; /*back to where we left off */ }

Methods Method: A Discrete Piece of Code that Performs a Specific Operation or Task Named with a Descriptive Identifier Called from main() or Another Method When Called, Program Control (Execution) Is Transferred to the method Method Performs Required Tasks, and then Possibly Returns a Value After Return from Method, Control Returns to the Statement Following the Method Call

Method Attributes Method Name: Identifier Used to Call method Method Parameter(s) or Argument(s): Value(s) Passed into method for Use by method Code Method Return Value: Value Returned by method Back to Calling method

Method Parameters (Arguments) May Pass as Many Parameters as Necessary to method A Copy of the Value of the Parameter Is Passed to the Method Changing the Value of the Parameter in the Method Does Not Affect the Value of the Original Variable This Is Called Pass-by-Value

Methods – Return Values Methods Are Typed According to Their Return Values: void, int, double, etc. Method Returns a Value to Calling method via return Statement

class BasicMethod { public static void main(String [ ] args) { int val = 9; System.out.println(“squareVal returned: " + squareVal(val)); } public static int squareVal(int numToSq) { System.out.println("In squareVal "); return(numToSq * numToSq); }

Classes A Class Is an Object Type A Class Represents a “Thing” (e.g., employee, wrench, list, store, shopping cart, etc.) Service Class – Class used by Other Programs Programmers Define Classes with Data Members (or Fields) and Methods Must Be Created in ClassName.java File

Object Declaration class useObject { public static void main(String [ ] args) { Object varName = new Object();//instance varName.objectMethod(); //invoke method }} Instance of Object Type Is Called Invoking Object of Class Methods varName is an instance of Object varName is invoking object of objectMethod()

Designing a Class Decide How It Will Be Used Decide on Interface (i.e., public representation, public methods) Decide on Implementation (i.e., private data and data manipulation) Example: String Methods (interface) includes length(), toUpper(), toLower(), charAt(), etc. Example: String Implementation includes (*probably*) char variables to hold characters, integer to hold length (we don’t actually need to know)

Constructor Class Method of Same Name Example: class Employee Method Employee() Called When Variable Declared (instantiated) of Class Type Initializes Instantiated Object May Have Multiple Constructors Each with Different Parameters

Access Modifiers Used to Identify What May Use Methods public – any method may use private – only methods in same class may use protected – only methods in same class or related classes may use.

Example: Class Employee String firstName; String lastName; double salary; Employee emp1; First and last names and salaries are attributes.

//Employee.java file class Employee { private String firstName; private String lastName; private double salary; private final double MAXSALARY = ; public Employee() { firstName = "NoFirstName"; lastName = "NoLastName"; salary = 0.0; }

Employee Declaration String firstName; String lastName; double salary; Employee emp1 = new Employee(); 0.0 "NoFirstName" "NoLasttName"

Accessor Methods Public Methods for Getting Attributes of Class

class Employee { private String firstName; private String lastName; private double salary; private final double MAXSALARY = ; public Employee() { firstName = "NoFirstName"; lastName = "NoLastName"; salary = 0.0; } public String GetFirstName() { return firstName; } public String GetLastName() { return lastName; } public double GetSalary() { return salary; }

Client Program A Program that Uses a Class Is a Client of that Class Class Objects are Declared and Instantiated Using the new keyword. new ClassName(parameters) Calls Constructor for Class Class Public Methods Called Using Dot (e.g., variable.GetName(); )

class useEmployee { public static void main(String [ ] args) { Employee emp1 = new Employee(); System.out.println("Name is" + emp1.GetFirstName() + " " + emp1.GetLastName()); }

Mutator Methods Class Methods Used to Modify a Class Object are Called Mutator Methods Allows Restricted Manipulation of Class Object Data

public boolean SetSalary(double passedSalary) { if (passedsalary <= MAXSALARY) { salary = passedSalary; return true; } else { return false; }

class useEmployee { public static void main(String [ ] args) { Employee emp1 = new Employee(); System.out.println("Name is" + emp1.GetFirstName() + " " + emp1.GetLastName()); if (emp1.SetSalary(55000)) { System.out.println("Salary set to 55000"); } else { System.out.println("Salary not set"); }

What If? Write a program that prompts for the names and locations of 1000 employees. Store the information in the program for later use.

Program for Previous String empName1, empName2, empName3,… String empLocation1, empLocation2, … System.out.print( “Enter employee name 1:”); empName1 = scan.next(); System.out.print(“Enter employee name 2:”); empName2 = scan.next(); … //Can we use a loop?

Arrays Syntax: type variableName[] = new type [size]; 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 char [] lastname = new char[100]; lastname[0] = ‘H’; lastname[1] = ‘a’; lastname[2] = ‘\0’; System.out.println( lastname[0]);

Array Example int [] values = new int[15]; values[0] = 150; values[1] = 78; values[2] = 16; 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; }

Array Example final int ARRAY_SIZE = 10; int offset, sum = 0, average; int [] numArray = new int[ARRAY_SIZE]; for(offset = 0; offset < ARRAY_SIZE; offset++) { System.out.print( “Enter Score ” + offset + “ : “); numArray[offset] = scan.nextInt(); } for(offset = 0; offset < ARRAY_SIZE; offset++) { sum = sum + numArray[offset]; } average = sum / ARRAY_SIZE;

Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())