Announcements - PowerPoint PPT Presentation

About This Presentation
Title:

Announcements

Description:

Announcements Final Exam:TBD – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 25
Provided by: Preferr750
Learn more at: http://www.cs.iit.edu
Category:

less

Transcript and Presenter's Notes

Title: Announcements


1
Announcements
  • Final ExamTBD

2
  • public static void main(String args)
  • final int ASIZE 5
  • int intArray new intASIZE
  • for(int i 0 i lt ASIZE i)
  • intArrayi i
  • System.out.println( intArray0)
  • changeZero(intArray)
  • System.out.println( intArray0)
  • static void changeZero(int passedArray)
  • passedArray0 1000

3
Initializing Arrays
  • char cArray3 'a', 'b', 'c'
  • int iArray 1, 2, 3, 4
  • double dArray 3.4, 5.67e4

4
Two Dimensional Arrays
  • char cArray new char1020
  • int iArray new int10050
  • cArray00 a
  • cArray01 b
  • cArray919 x
  • iArray00 99
  • iArray15 135
  • iArray9949 0

5
Math Class
  • Contains Typical Math Methods
  • In Package java.lang (i.e., automatically
    imported)
  • Access using Math.math_thing
  • Math.PI Math.E (log e 1)
  • double pow (double base, double exp)
  • double sqrt(double number)
  • int round(double number)
  • int abs( int number)
  • min(number1,number2) and max(number1, number2)
  • double random() //random number between 0.0 and
    1.0

6
Tokenizing/Parsing
  • Taking Apart a String and Separating It into
    Smaller Strings (i.e., tokens)
  • Usually, Tokens are Items Separated by Whitespace
  • Tokens Can Be Defined with Different Separators
  • Parsing Takes Tokens and Applies Some
    Manipulation of Those Tokens

7
StringTokenizer Class
  • import java.util.
  • String bigString This is a sentence.
  • StringTokenizer st new StringTokenizer(bigString
    )
  • while (st.hasMoreTokens())
  • System.out.println(st.nextToken())
  • // Displays This, is, a, and sentence. one
  • // per line

8
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
  • Client Program Program using a class

9
Access Modifiers
  • Used to Identify What May Use Methods
  • public any method may use (Usually methods)
  • private only methods in same class may use
    (usually data members)

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

11
Example Class
Class Employee
firstName
lastName
salary
12
Constructor
  • Class Method of Same Name
  • Example class Employee Method Employee()
  • Called When Variable (invoking object) Declared
    (instantiated) of Class Type
  • Initializes Instantiated Object
  • May Have Multiple Constructors Each with
    Different Parameters

13
  • class Employee // Employee.java
  • private String firstName
  • private String lastName
  • private double salary
  • public Employee()
  • // set attributes of invoking object
  • firstName "NoFirstName"
  • lastName "NoLastName"
  • salary 0.0

14
Accessor Methods
  • Public Methods for Getting Attributes of Class

15
  • class Employee
  • private String firstName
  • private String lastName
  • private double salary
  • public Employee()
  • firstName "NoFirstName"
  • lastName "NoLastName"
  • salary 0.0
  • public String GetFirstName()
  • return firstName
  • public String GetLastName()
  • return lastName

16
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.GetFirstName())
  • variable is the invoking object

17
  • class useEmployee //useEmployee.java
  • public static void main(String args)
  • Employee emp1 new Employee()
  • System.out.println("Name is"
    emp1.GetFirstName() " " emp1.GetLastName())

firstName
emp1
NoFirstName
lastName
NoLastName
salary
0.0
18
Mutator Methods
  • Class Methods Used to Modify a Class Object are
    Called Mutator Methods
  • Allows Restricted Manipulation of Class Object
    Data

19
  • public boolean SetSalary(double passedSalary)
  • if (passedSalary lt MAXSALARY)
  • salary passedSalary
  • return true
  • else
  • return false

20
  • 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")

21
Static Variables and Methods
  • static means in class methods and variables
  • static variable one per class (not one per
    object)
  • static method no invoking object (invoke with
    className.method())
  • Example Math class methods

22
Static Variable Example
  • public class MyClass
  • private final int STARTID 1
  • //the following are one per object
  • private int IDNum -1
  • //the following are one per class
  • private static int nextIDNum STARTID
  • public MyClass()
  • IDNum nextIDNum
  • nextIDNum

23
Static Method Example
  • public class MyClass
  • private final int STARTID 1
  • //the following are one per object
  • private int IDNum -1
  • //the following are one per class
  • private static int nextIDNum STARTID
  • public static int GetNextID()
  • // Called by MyClass.GetNextID()
  • return nextIDNum

24
Other Class Methods
  • boolean equals( objType comObj)
  • compares invoking object with passed object
  • Returns true if both are equal, false if not
  • void display()
  • Displays attributes
  • String toString()
  • Converts all attributes to String and returns
    String
Write a Comment
User Comments (0)
About PowerShow.com