Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMPUTER 2430 Object Oriented Programming and Data Structures I

Similar presentations


Presentation on theme: "COMPUTER 2430 Object Oriented Programming and Data Structures I"— Presentation transcript:

1 COMPUTER 2430 Object Oriented Programming and Data Structures I
Using NetBeans for class Object methods toString and equals (no programs on inheritance now) 20 minutes for Quiz 1

2 Java String // Data type String is a class!
// All class names begin with a capital char String firstName, lastName, str; // Get a String object firstName = new String("first"); str = "First"; // same as // str = new String("First"); We do not need this: import java.lang.*; // Not just for String

3 Operator + String str = "First"; int x = 10;
// str = str + "appends to a string"; str += "appends to a string"; // "Firstappends to a string" str += " need a space here“; // "Firstappends to a string need a space here" str = str + ',' + x; // "Firstappends to a string need a space here,10“ // str += ',' + x; might be different!

4 Method length() public class String { . . . /**
Returns the number of chars in "this" string Sample code: int count = str.length(); */ public int length() } Both are instance methods

5 Method equals() public class String { . . . /**
Returns true when "this" string has the same sequence of chars as string s false otherwise Sample code: if ( str1.equals(str2) ) */ public boolean equals( String s ) } Both are instance methods

6 Example String str1, str2; Scanner sc = new Scanner( System.in );
str1 = new String("first"); str2 = sc.next(); // next string (token) if (str1.equals(str2)) System.out.println("str1 is the same as str2."); else System.out.println("str1 is not the same as str2."); System.out.println("str1 has " + str1.length() + " chars.");

7 Java String str1 = new String("first");
str2 = sc.next(); // input: first if (str1.equals(str2)) System.out.println("equal."); else System.out.println("not equal."); if (str1 == str2) System.out.println("Same string."); System.out.println("Not the same string.");

8 ADT: Student Data Name, Address, Phone, Email DOB GPA . . . Operations
Set/Get name

9 ADT: Professor Data Name, Address, Phone, Email DOB Tenured . . .
Operations Set/Get name

10 Features of OOP Abstraction Encapsulation Data Hiding Inheritance
Polymorphism

11 ADT: Person Data Operations Name, Address, Phone, Email DOB . . .
Set/Get name

12 public class Person { private String name, address, phone, ; private Date dob; . . . public boolean setName (String s) boolean valid = false; if (s.length() > 1) name = s; valid = true; } return valid; public String getName() return name; ...

13 /** Will inherit all members from class Person, including name and the two methods on name public boolean setName (String s) public String getName() */ public class Student extends Person { private float gpa; public boolean setGPA (float value) public float getGPA() . . . }

14 /** Will inherit all members from class Person, including name and the two methods on name public boolean setName (String s) public String getName() */ public class Professor extends Person { private boolean tenured; public boolean applyGrant (String grantName) public void teachCourse (String courseName) . . . }

15 public class CS2430Sx { public static void main(String[] args) Student std = new Student(); std.setName("Frank"); System.out.println("Student: " + std.getName()); Professor prof = new Professor(); prof.setName("Qi"); System.out.println("Professor: " + prof.getName()); }

16 Features of OOP Abstraction Encapsulation Data Hiding Inheritance
Polymorphism

17 Prog 1 Due 10 pm, Friday, September 14
-2 by 10 pm, Saturday, September 15 -5 by 10 pm, Monday, September 17 No credit after 10 pm, September 17 Still have to complete the program!

18 Lab 2 Five (5) points Due 10 pm, Friday, September 21

19 Last Day to Drop Classes

20 Quiz 1 30 Minutes Close book Close notes Close computer


Download ppt "COMPUTER 2430 Object Oriented Programming and Data Structures I"

Similar presentations


Ads by Google