Exam 2 EXAM 2 Thursday 25 of Final

  • Slides: 32
Download presentation
Exam 2 • • • EXAM 2 Thursday!!! 25% of Final Grade Know: loops,

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

Files Review • For output to a file: – File. Output. Stream variable initialized

Files Review • For output to a file: – File. Output. Stream variable initialized to filename (String) and append (boolean) – Print. Writer variable initialized to File. Output. Stream variable • For input from file: – File variable initialized to filename (String) – Scanner variable initialized to File variable

import java. util. Scanner; import java. io. *; mydata. txt file class Format. File.

import java. util. Scanner; import java. io. *; mydata. txt file class Format. File. Data { public static void main(String [ ] args) throws IOException { int loops, integer, i; float decimal; String name; 5 8 9. 3 Jon 6 14. 335 Bill 0 35. 67 e 9 Mary -23 -4. 55 Smith -3 -4 e 3 xyz File ifile = new File("mydata. txt"); Scanner scan = new Scanner(ifile); loops = scan. next. Int(); for(i= 0 ; i < loops; i++) { integer = scan. next. Int(); decimal = scan. next. Float(); name= scan. next(); System. out. print(integer + " "); System. out. print(decimal + " "); System. out. print(name + " "); System. out. println(); } Output: 8 9. 3 Jon 6 14. 335 Bill 0 3. 567 E 10 Mary -23 -4. 55 Smith -3 – 4000. 0 xyz

Methods public static void main(String [] args) { … display. Vals(); … … display.

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

Methods • Method: A Discrete Piece of Code that Performs a Specific Operation or

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

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 •

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,

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 Basic. Method { public static void main(String [ ] args) { int val

class Basic. Method { public static void main(String [ ] args) { int val = 9; System. out. println(“square. Val returned: " + square. Val(val)); } public static int square. Val(int num. To. Sq) { System. out. println("In square. Val "); return(num. To. Sq * num. To. Sq); } }

Classes • A Class Is an Object Type • A Class Represents a “Thing”

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 Class. Name. java File

Object Declaration class use. Object { public static void main(String [ ] args) {

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

Designing a Class • Decide How It Will Be Used • Decide on Interface

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(), to. Upper(), to. Lower(), char. At(), 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() •

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

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 emp 1; String first. Name; String last. Name; double salary; First

Example: Class Employee emp 1; String first. Name; String last. Name; double salary; First and last names and salaries are attributes.

//Employee. java class Employee { private String private double first. Name; last. Name; salary;

//Employee. java class Employee { private String private double first. Name; last. Name; salary; private final double MAXSALARY = 2000000; public Employee() { first. Name = "No. First. Name"; last. Name = "No. Last. Name"; salary = 0. 0; }

Employee Declaration Employee emp 1 = new Employee(); String first. Name; "No. First. Name"

Employee Declaration Employee emp 1 = new Employee(); String first. Name; "No. First. Name" String last. Name; "No. Lastt. Name" double salary; 0. 0

Accessor Methods • Public Methods for Getting Attributes of Class

Accessor Methods • Public Methods for Getting Attributes of Class

class Employee { private String first. Name; private String last. Name; private double salary;

class Employee { private String first. Name; private String last. Name; private double salary; private final double MAXSALARY = 2000000; public Employee() { first. Name = "No. First. Name"; last. Name = "No. Last. Name"; salary = 0. 0; } public String Get. First. Name() { return first. Name; } public String Get. Last. Name() { return last. Name; } public double Get. Salary() { return salary; }

Client Program • A Program that Uses a Class Is a Client of that

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 Class. Name(parameters) Calls Constructor for Class • Class Public Methods Called Using Dot (e. g. , variable. Get. Name(); )

class use. Employee { public static void main(String [ ] args) { Employee emp

class use. Employee { public static void main(String [ ] args) { Employee emp 1 = new Employee(); System. out. println("Name is" + emp 1. Get. First. Name() + " " + emp 1. Get. Last. Name()); } }

Mutator Methods • Class Methods Used to Modify a Class Object are Called Mutator

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

public boolean Set. Salary(double passed. Salary) { if (passedsalary <= MAXSALARY) { salary =

public boolean Set. Salary(double passed. Salary) { if (passedsalary <= MAXSALARY) { salary = passed. Salary; return true; } else { return false; } }

class use. Employee { public static void main(String [ ] args) { Employee emp

class use. Employee { public static void main(String [ ] args) { Employee emp 1 = new Employee(); System. out. println("Name is" + emp 1. Get. First. Name() + " " + emp 1. Get. Last. Name()); if (emp 1. Set. Salary(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

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 emp. Name 1, emp. Name 2, emp. Name 3, …

Program for Previous String emp. Name 1, emp. Name 2, emp. Name 3, … String emp. Location 1, emp. Location 2, … System. out. print( “Enter employee name 1: ”); emp. Name 1 = scan. next(); System. out. print(“Enter employee name 2: ”); emp. Name 2 = scan. next(); … //Can we use a loop?

Arrays • Syntax: type variable. Name[] = new type [size]; • Syntax: type [

Arrays • Syntax: type variable. Name[] = new type [size]; • Syntax: type [ ] variable. Name = 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’;

Array Example char [] lastname = new char[100]; lastname[0] = ‘H’; lastname[1] = ‘a’; lastname[2] = ‘’; System. out. println( lastname[0]);

Array Example int [] values = new int[15]; values[0] = 150; values[1] = 78;

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 [] num. Array =

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

Array Example final int ARRAY_SIZE = 10; int offset, sum = 0, average; int

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

Exam 2 • • • EXAM 2 Thursday!!! 25% of Final Grade Know: loops,

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