Week8Chap5-II

Week8Chap5-II - Chapter 5 More about Writing Classes Janaka...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Janaka Balasooriya CSE 110, Arizona State University Chapter 5 More about Writing Classes
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Janaka Balasooriya CSE 110, Arizona State University Writing Classes: Quick Recap • Object designing process • attributes and methods • private vs. public UML diagrams • Implementation • constructors • value returning methods void methods • Creating object instances • use constructors with or without parameters • object diagram and object instance data
Background image of page 2
1-3 toString() method Consider the Car class that we have developed. public class Car { private double enginePower; private String color; private int speed; public Car() { enginePower = 3.2; color = "White"; speed = 0; } public void speedUp(int sp) { speed = sp; } public void paint(String newColor) { color = newColor; System.out.println("Your car has been painted " + newColor); } public void displayStatus() { System.out.println("Your car has the engine power " + enginePower ); System.out.println("Currently it is running " + speed + " miles per hour"); System.out.println("Its color is " + color); } } import java.util.Scanner; public class CarDriver { public static void main(String [] args) { String myColor; int speed; Car myCar = new Car(); Scanner myScan = new Scanner(System.in); System.out.println("Enter your favorite car color:"); myColor=myScan.nextLine(); myCar.paint(myColor); System.out.println("Enter new speed:"); speed=myScan.nextInt(); myCar.speedUp(speed); System.out.println(myCar); } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
1-4 toString() method In order to pass an object into System.out.println() method, first we need to implement the toString() method of that object. public class Car { ……………. . public String toString() { return "Your car has the engine power " + enginePower + “Currently it is running” + speed + " miles per hour“ + “Its color is” + color; } …………. . } import java.util.Scanner; public class CarDriver { public static void main(String [] args) { String myColor; int speed; Car myCar = new Car(); Scanner myScan = new Scanner(System.in); System.out.println("Enter your favorite car color:"); myColor=myScan.nextLine(); myCar.paint(myColor); System.out.println("Enter new speed:"); speed=myScan.nextInt(); myCar.speedUp(speed); System.out.println(myCar); } }
Background image of page 4
1-5 Encapsulation We can take one of two views of an object internal - the details of the variables and methods of the class that defines it external - the services that an object provides and how the object interacts with the rest of the system
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

Week8Chap5-II - Chapter 5 More about Writing Classes Janaka...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online