+ All Categories
Home > Documents > COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Date post: 18-Jan-2016
Category:
Upload: ami-glenn
View: 215 times
Download: 0 times
Share this document with a friend
34
COMP 110 COMP 110 Classes Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014
Transcript
Page 1: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

COMP 110COMP 110ClassesClasses

Luv KohliOctober 1, 2008

MWF 2-2:50 pmSitterson 014

Page 2: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

AnnouncementsAnnouncementsLab 4 due

2

Page 3: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Questions?Questions?

3

Page 4: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Today in COMP 110Today in COMP 110Briefly go over Program 2

Classes

4

Page 5: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Program 2Program 2

5

1 2 . 7 + 3 . 30 1 2 3 4 5 6 7 8 9

Operand 1 Operand 2Operator

Page 6: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Program 2Program 2

6

1 2 . 7 + 3 . 30 1 2 3 4 5 6 7 8 9

Assume the String above is in the variable calculation.

int firstSpace = calculation.indexOf(‘ ’);int lastSpace = calculation.lastIndexOf(‘ ’);

double operand1 = Double.parseDouble(calculation.substring(0, firstSpace));double operand2 = Double.parseDouble(calculation.substring(lastSpace + 1));char operator = calculation.charAt(firstSpace + 1);

Page 7: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Program 2Program 2Now you can determine which calculation to

perform using a switch or if/else

double result = 0.0;

switch (operator)

{

case ‘+’:

result = operand1 + operand2;

break;

case ‘-’:

result = operand1 – operand2;

break;

// and so on

}

7

Page 8: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Exponent ^Exponent ^double result = 0.0;

switch (operator)

{

case ‘^’:

result = 1.0;

for (int count = 0; count < (int) operand2; count++)

result *= operand1;

break;

case ‘-’:

result = operand1 – operand2;

break;

// and so on

}

8

Page 9: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

== generally bad for floating-point== generally bad for floating-point

Floating-point numbers are imprecise◦After doing many computations, value may

not be exactly the same◦5.0 * 3.0 might be different from

1.0 + 2.0 + 3.0 + 4.0 + 5.0Okay for this assignment for the divide by

0 extra credit

9

Page 10: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

num++ vs. ++numnum++ vs. ++numnum++ is NOT num + 1num++ does num = num + 1So does ++num, BUT, there is a difference

int num1 = 5;

System.out.println(num1++);

// outputs num1 (5), then increments num1

int num2 = 5;

System.out.println(++num2);

// increments num2, then outputs num2 (6)

10

Page 11: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

ClassClassA class is the definition of a kind of object

◦A blueprint for constructing specific objects

11

Class Name: Automobile

Data: amount of fuel speed license plate

Methods (actions): accelerate: How: Press on gas pedal. decelerate: How: Press on brake pedal.

Page 12: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Objects, InstantiationObjects, Instantiation

12

Object Name: patsCar

amount of fuel: 10 gallonsspeed: 55 miles per hourlicense plate: “135 XJK”

Object Name: suesCar

amount of fuel: 14 gallonsspeed: 0 miles per hourlicense plate: “SUES CAR”

Object Name: ronsCar

amount of fuel: 2 gallonsspeed: 75 miles per hourlicense plate: “351 WLF”

Instantiations, or instances, of the class Automobile

Page 13: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

UML (Universal Modeling Language)UML (Universal Modeling Language)

13

Automobile

- fuel: double- speed: double- license: String

+ accelerate(double pedalPressure): void+ decelerate(double pedalPressure): void

Class name

Data

Methods (actions)

Page 14: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

ObjectsObjectsImportant: classes do not have data;

individual objects have dataClasses specify what kind of data objects

have

14

Page 15: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Class files and separate compilationClass files and separate compilation

Each Java class definition goes in its own, SEPARATE .java file

ClassName save the file as ClassName.java

Student.java includes the class Student

15

Page 16: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Class files and separate compilationClass files and separate compilation

What happens when you compile a .java file?◦ .java file gets compiled into a .class file

Contains Java bytecode Same filename except for .class instead of .java (note to self: show this)

You can compile a Java class before you have a program that uses it

16

Page 17: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

class Studentclass Student

17

Class Name: Student

- Name- Year- GPA- Major- Credits- GPA sum

+ getName+ getMajor+ printData+ increaseYear How: increase year by 1+ calcGpa How: average grades

Page 18: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

class Studentclass Student

18

Class Name: Student

- name: String- year: int- gpa: double- major: String- credits: int- gpaSum: double

+ getName(): String+ getMajor(): String+ printData(): void+ increaseYear(): void+ calcGpa(double grade): void

Page 19: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Defining a classDefining a classpublic class Student{ public String name; public int classYear; public double GPA; public String major; // ...

public String getMajor() { return major; }

public void increaseYear() { classYear++; }}

19

Class name

Data(instance variables)

Methods

Page 20: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Creating an objectCreating an objectCreate an object jack of class StudentStudent jack = new Student();

Scanner keyboard = new Scanner(System.in);

Create an object keyboard of class Scanner

20

Create an objectReturn memory address of object

Assign memory address of object to variable

Page 21: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Instance variablesInstance variablesData defined in the class are called

instance variables

public String name; public int classYear; public double GPA; public String major;

21

public: no restrictions on how these instance variables are used (more details later – public is actually a bad idea here)

type: int, double, String…

variables

Page 22: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Using instance variables: inside the class definitionUsing instance variables: inside the class definition

22

public class Student{ public String name; public int classYear; public double GPA; public String major; // ...

public String getMajor() { return major; }

public void increaseYear() { classYear++; }}

Page 23: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Using Using publicpublic instance variables outside a class instance variables outside a class

public static void main(String[] args){ Student jack = new Student(); jack.name = “Jack Smith”; jack.major = “Computer Science”;

System.out.println(jack.name + “ is majoring in ” + jack.major);

Student apu = new Student(); apu.name = “Apu Nahasapeemapetilon”; apu.major = “Biology”;

System.out.println(apu.name + “ is majoring in ” + apu.major);}

jack.name and apu.name are two different instance variables because they belong to different objects

23

Page 24: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

MethodsMethodsTwo kinds of methods

◦Methods that return a value Examples: String’s .substring() method,

String’s .indexOf() method, etc.

◦Methods that return nothing Example: System.out.println()

24

Page 25: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

MethodsMethods

public String getMajor(){ return major;}

public void increaseYear(){ classYear++;}

25

returns a String

returns nothing

return type

Page 26: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Defining methods that return nothingDefining methods that return nothing

Method heading: keywords◦public: no restriction on how to use the

method (more details later)◦void: the method returns nothing

Method body: statements executed when the method is called (invoked)◦Must be inside a pair of braces

public void increaseYear(){ classYear++;}

26

Page 27: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Method printDataMethod printDataAs usual, inside a block (defined by

braces), you can have multiple statements

public void printData(){ System.out.println(“Name: ” + name); System.out.println(“Major: ” + major); System.out.println(“GPA: ” + gpa);}

27

Page 28: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Calling methods that return nothingCalling methods that return nothing

object, followed by dot, then method name, then ()

Use them as Java statementsStudent jack = new Student();jack.classYear = 1;

jack.increaseYear();

System.out.println(“Jack’s class year is ” + jack.classYear);

28

Page 29: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Defining methods that return a valueDefining methods that return a value

Method heading: keywords◦public: no restriction on how to use the

method (more details later)◦Type: the type of value the method returns

Method body: statements executed◦Must be inside a pair of braces◦Must have a return statement

public String getMajor(){ return major;}

29

Page 30: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

returnreturn statement statementA method that returns a value must have

at least one return statementTerminates the method, and returns a

value to the callerSyntax:

◦return Expression;Expression can be any expression that

produces a value of type specified by the return type in the method heading

30

Page 31: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Methods that return a valueMethods that return a valuepublic String getClassYear(){ if (classYear == 1) return “Freshman”; else if (classYear == 2) return “Sophomore”; else if ...}

31

Page 32: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

Calling methods that return a valueCalling methods that return a value

object, followed by dot, then method name, then () (same as before)

Use them as a value of the type specified by the method’s return type

Student jack = new Student();jack.major = “Computer Science”;

String m = jack.getMajor();

System.out.println(“Jack’s full name is ” + jack.getName());

System.out.println(“Jack’s major is ” + m);

32

Page 33: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

returnreturn statement statementCan also be used in methods that return

nothingTerminates the methodSyntax:

◦return;

public void increaseYear(){ if (classYear >= 4) return; classYear++;}

33

Page 34: COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.

FridayFridayMore about classes

3434


Recommended