Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 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

3 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 8 9.3 Jon 6 14.335 Bill 0 3.567E10 Mary -23 -4.55 Smith -3 –4000.0 xyz Output:

4 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 */ }

5 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

6 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

7 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

8 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

9 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); }

10 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

11 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()

12 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)

13 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

14 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.

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

16 //Employee.java file 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; }

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

18 Accessor Methods Public Methods for Getting Attributes of Class

19 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; }

20 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(); )

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

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

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

24 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"); }

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

26 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?

27 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

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

29 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;

30 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; }

31 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;

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


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

Similar presentations


Ads by Google