{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Intro to Java Web-Notes_Part14

Intro to Java Web-Notes_Part14 - Bus trainStation...

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

View Full Document Right Arrow Icon
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Bus trainStation; trainStation = new Bus(); Once the programmer writes a constructor, the default constructor is no longer automatically added. If we write a constructor that takes a value then the default "no argument" constructor is no longer written for us (we will have to write that ourselves). The following code won't compile because the main method in the Driver class is creating objects where a value is passed to the constructor (the values 3 and 10) and also tries to use a constructor where no value is passed (i.e., "new Bus()"): class Bus { int passengers; public Bus (int startPassengers) { passengers = startPassengers; } } public class Driver { public static void main(S:ring args) { Bus routel : new 3us(3); Bus route2 = new Bus(lO); Bus route3 = new 3us(); // this line won't compile If we want to be able to create Bus objects both by specifying the initial number of passengers, and also by just letting the number of passengers default to zero, then we need to have two constructors: class Bus { int passengers; public Bus () { passengers : O; } public Bus (int startPassengers) { passengers : startPassengers; } } public class Driver { public static void main(S:ring args) { new 3us(3); new Bus(lO); new 3us(); Bus routel Bus route2 Bus route3 The above code will work properly -- the first two instantiations, "new Bus(3)” and "new Bus(lO)", use the constructor that receives an input value as startPassengers, and the last instantiation, "new Bus()", uses the "no argument" constructor. Pulling It All Together Here is a short program in which the Sample class creates several Bus objects. The visualization shown below the code illustrates the action of the constructors and the methods. This example has a Bus class with the constructors talked about above, plus the oneMoreA board and everyOfl methods, and new method report that prints a short message to the console. class Bus { private int passengers; public Bus () passengers = 0; public Bus (int startPassengers) passengers : startPassengers; public void oneMoreAboard() passengers++; public void everyoneOff() passengers : 0; public void report() System.out.println("Passengers on this bus: " + passengers); } public class Sample { public static void main(S:ring args) { Bus routel = new 3us(22); Bus route2 = new 3us(4); Bus route3 : new Bus(); rOL:el.oneMoreAboard(); rot:el.report(); rOL:e2.oneMoreAboard(); rOL:e2.oneMoreAboard(); rOL:e2.oneMoreAboard(); rot:e3.oneMoreAboard(); rOL:el.everyoneOff(); rOL:el.oneMoreAboard(); rot:el.report(); When the code runs the following message shows up on the console: Passengers on this bus: 23 Passengers on this bus: 1 Only the data values are shown inside the objects this visualization of the above code. ...
View Full 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