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

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

Janaka Balasooriya CSE 110, Arizona State University Chapter 5 More about Writing Classes
Image of page 1

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

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
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); } }
Image of page 3

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

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); } }
Image of page 4
1-5 Encapsulation
Image of page 5

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

Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern