Exam 2 EXAM 2 Thursday!!! 25% of Final Grade

Slides:



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

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.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
Lecture 2: Object Oriented Programming I
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
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.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
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.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Written by: Dr. JJ Shepherd
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.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Chapter 9 Introduction to Arrays Fundamentals of Java.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
(Dreaded) Quiz 2 Next Monday.
Objects as a programming concept
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
3 Introduction to Classes and Objects.
Examples of Classes & Objects
Yanal Alahmad Java Workshop Yanal Alahmad
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 3: Using Methods, Classes, and Objects
Selenium WebDriver Web Test Tool Training
Quiz Next Monday.
An Introduction to Java – Part I
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.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Classes Variables That Are Not of a Built-in Type Are Objects
Chapter 3 Introduction to Classes, Objects Methods and Strings
An Introduction to Java – Part I, language basics
Classes & Objects: Examples
Group Status Project Status.
Know for Quiz Everything through Last Week and Lab 7
File Review Declare the File Stream Object Name
Arrays of Objects // The following does NOT create memory for
OBJECT ORIENTED PROGRAMMING II LECTURE 13_2 GEORGE KOUTSOGIANNAKIS
CIS 199 Final Review.
Review for Midterm 3.
Introduction to java Part I By Shenglan Zhang.
CS 240 – Advanced Programming Concepts
Presentation transcript:

Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Everything through Last Week, Lab 9 Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt()) Arrays (declaration, access)

Files Review For output to a file: For input from 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

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

Methods public static void main(String [] args) public static void { … 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 Employee emp1; String firstName; String lastName; double salary; First and last names and salaries are attributes.

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

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

Accessor Methods Public Methods for Getting Attributes of Class

public String GetFirstName() return firstName; class Employee { private String firstName; private String lastName; private double salary; private final double MAXSALARY = 2000000; 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");

Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Everything through Last Week, Lab 9 Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt()) Arrays (declaration, access)