Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Exam 2 EXAM 2 Thursday!!! 25% of Final Grade"— Presentation transcript:

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

2 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

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

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

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
Employee emp1; String firstName; String lastName; double salary; First and last names and salaries are attributes.

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

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

18 Accessor Methods Public Methods for Getting Attributes of Class

19 public String GetFirstName() return firstName;
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;

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


Download ppt "Exam 2 EXAM 2 Thursday!!! 25% of Final Grade"

Similar presentations


Ads by Google