1 / 35

COMP 110 Classes

COMP 110 Classes. Catie Welsh February 21, 2011. Announcements. Lab 4 due on Friday. Questions?. Today in COMP 110. Briefly go over Strings and Loops Worksheet Briefly go over Program 2 Classes. Program 2. Operand 1. Operator. Operand 2. Program 2.

dara-burke
Download Presentation

COMP 110 Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMP 110Classes Catie Welsh February 21, 2011

  2. Announcements • Lab 4 due on Friday

  3. Questions?

  4. Today in COMP 110 • Briefly go over Strings and Loops Worksheet • Briefly go over Program 2 • Classes

  5. Program 2 Operand 1 Operator Operand 2

  6. Program 2 Assume the String above is in the variable calculation. intfirstSpace = calculation.indexOf(‘ ’); intlastSpace = calculation.lastIndexOf(‘ ’); double operand1 = Double.parseDouble(calculation.substring(0, firstSpace)); double operand2 = Double.parseDouble(calculation.substring(lastSpace + 1)); char operator = calculation.charAt(firstSpace + 1);

  7. Program 2 • Now 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 }

  8. Exponent ^ double result = 0.0; switch (operator) { case ‘^’: result = 1.0; if(operand2 > 0) { for (int count = 0; count < (int) operand2; count++) result *= operand1; } else if(operand2 < 0) { for (int count = 0; count > (int) operand2; count--) result *= operand1; result = 1.0 / result; } break; case ‘-’: result = operand1 – operand2; break; // and so on }

  9. == 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 from1.0 + 2.0 + 3.0 + 4.0 + 5.0 • Okay for this assignment for the divide by 0 error checking

  10. num++ vs. ++num • num++ is NOT num + 1 • num++ does num = num + 1 • So 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)

  11. Classes and Objects • Java programs (and programs in other object-oriented programming languages) consist of objects of various class types • Objects can represent objects in the real world • Automobiles, houses, employee records • Or abstract concepts • Colors, shapes, words

  12. Class • A class is the definition of a kind of object • A blueprint for constructing specific objects Class Name: Automobile Data: amount of fuel speed license plate Methods (actions): accelerate: How: Press on gas pedal. decelerate: How: Press on brake pedal.

  13. Objects, Instantiation Object Name: patsCar amount of fuel: 10 gallons speed: 55 miles per hour license plate: “135 XJK” Object Name: suesCar amount of fuel: 14 gallons speed: 0 miles per hour license plate: “SUES CAR” Object Name: ronsCar amount of fuel: 2 gallons speed: 75 miles per hour license plate: “351 WLF” Instantiations, or instances, of the class Automobile

  14. UML (Universal Modeling Language) Class name Data Methods (actions)

  15. Objects • Important: classes do not have data; individual objects have data • Classes specify what kind of data objects have

  16. Class 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

  17. Class 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 • You can compile a Java class before you have a program that uses it

  18. class Student

  19. class Student

  20. Defining a class Class name public class Student { public String name; public intclassYear; public double GPA; public Stringmajor; // ... public StringgetMajor() { return major; } public void increaseYear() { classYear++; } } Data (instance variables) Methods

  21. Creating an object Create an object jack of class Student Student jack = new Student(); Scanner keyboard = new Scanner(System.in); Create an object keyboard of class Scanner Assign memory address of object to variable Return memory address of object Create an object

  22. Instance variables • Data defined in the class are called instance variables public String name; public int classYear; public double GPA; public Stringmajor; variables public: no restrictions on how these instance variables are used (more details later – public is actually a bad idea here) type: int, double, String…

  23. Using instance variables: inside the class definition public class Student { public String name; public intclassYear; public double GPA; public Stringmajor; // ... public StringgetMajor() { return major; } public void increaseYear() { classYear++; } }

  24. Using public 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

  25. Methods • Two 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()

  26. Methods public StringgetMajor() { return major; } public void increaseYear() { classYear++; } returns a String return type returns nothing

  27. Defining 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++; }

  28. Method printData • As 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); }

  29. Calling methods that return nothing • object, followed by dot, then method name, then () • Use them as Java statements Student jack = new Student(); jack.classYear = 1; jack.increaseYear(); System.out.println(“Jack’s class year is ” + jack.classYear);

  30. Defining 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 StringgetMajor() { return major; }

  31. return statement • A method that returns a value must have at least onereturn statement • Terminates the method, and returns a value to the caller • Syntax: • returnExpression; • Expression can be any expression that produces a value of type specified by the return type in the method heading

  32. Methods that return a value publicStringgetClassYear() { if(classYear == 1) return“Freshman”; elseif(classYear == 2) return“Sophomore”; elseif ... }

  33. Calling 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);

  34. return statement • Can also be used in methods that return nothing • Terminates the method • Syntax: • return; public void increaseYear() { if (classYear >= 4) return; classYear++; }

  35. Wednesday • More about classes 35

More Related