Csit115 hw5 solution | Computer Science homework help

1.       CH 9, self-check: Problem 1, pg. 634

What is code reuse?  How does inheritance help achieve code reuse?

2.       CH 9, self-check: problem 3, pg. 634

Which of the following is the correct syntax to indicate that class A is a subclass of B?

a. public class B extends A {

b. public class A : super B {

c. public A(super B) {

d. public class A extends B {

e. public A implements B {

3.       CH 9, self-check: problem 4, pg. 635

Consider the following classes:

public class Vehicle {…}

public class Car extends Vehicle {…}

public class SUV extends Car {…}

Which of the following are legal statements (assuming these classes all have constructors with no arguments)?

a. Vehicle v = new Car();

b. Vehicle v = new SUV();

c. Car c = new SUV();

d. SUV s = new SUV();

e. SUV s = new Car();

f. Car c = new Vehicle();

4.       CH 9, self-check: problem 5, pg. 635

Explain the difference between the this keyword and the super keyword.  When should each be used?

5.       CH 9, self-check: problem 6, pg. 635

For the next three problems consider the following class:

// Represents a university student.

public class Student {

     private String name;

     private int age;

 

     public Student(String name, int age) {

          this.name = name;

          this.age = age;

     }

 

     public void setAge(int age) {

          this.age = age;

     }

}

 

Also consider the following partial implementation of a subclass of Student to represent undergraduate students at a university:

public class UndergraduateStudent extends Student {

     private int year;

     . . .

}

 

Can the code in the UndergraduateStudent class access the name and age fields it inherits from Student?  Can it call the setAge method?

6.       CH 9, self-check: problem 11, pg. 637

Assume that the following classes have been defined (from section 9.3, pp. 600-601):

public class A {

     public void method1() {

          System.out.println(“A 1”);

`    }

    

     public void method2() {

          System.out.println(“A 2”);

     }

 

     public String toString() {

          return “A”;

     }

}

 

public class B extends A {

     public void method2() {

          System.out.println(“B 2”);

     }

}

 

public class C extends A {

     public void method1() {

          System.out.println(“C 1”);

     }

 

     public String toString() {

          return “C”;

     }

}

 

public class D extends C {

     public void method2() {

          System.out.println(“D 2”);

     }

}

 

What is the output produced by the following code fragment?

public static void main(String[] args) {

     A[] elements = {new B(), new D(), new A(), new C()};

     for (int i = 0; i < elements.length; i++) {

          elements[i].method2();

          System.out.println(elements[i]);

          elements[i].method1();

          System.out.println();

     }

}

 

7.       CH 9, self-check: problem 12, pp. 637-638

Assume that the following classes have been defined:

public class Flute extends Blue {

     public void method2() {

          System.out.println(“flute 2”);

     }

 

     public String toString() {

          return “flute”;

     }

}

 

public class Blue extends Moo {

     public void method1() {

          System.out.println(“blue 1”);

`    }

}

 

public class Shoe extends Flute {

     public void method1() {

          System.out.println(“shoe 1”);

`    }

}

 

public class Moo {

     public void method1() {

          System.out.println(“moo 1”);

`    }

    

     public void method2() {

          System.out.println(“moo 2”);

     }

 

     public String toString() {

          return “moo”;

     }

}

 

What is the output produced by the following code fragment?

public static void main(String[] args) {

     Moo[] elements = {new Shoe(), new Flute(), new Moo(), new Blue()};

     for (int i = 0; i < elements.length; i++) {

          System.out.println(elements[i]);

          elements[i].method1();

          elements[i].method2();

          System.out.println();

     }

}

 

8.       CH 9, exercises: problem 1, pg. 642.

Write the class Marketer to accompany the other law firm classes described in this chapter.  Marketers make $50,000 ($10,000 more than general employees) and have an additional method called advertise that prints “Act now, while supplies last!”  Make sure to interact with the superclass as appropriate.  Note that the textbook website has a Marketer.java, but it’s not quite right to fit with Employee.java in the text itself, so fix it up to do so.

9.       CH 9, exercises: Problem 5, pg. 643.

For the next two problems, consider the task of representing tickets to campus events.  Each ticket has a unique number and a price.  There are three types of tickets: walk-up tickets, advance tickets, and student advance tickets.  See the class diagram below:

http://www.cs.umb.edu/%7Etolkien/csit115/hw5_files/image001.jpg

·         Walk-up tickets are purchased the day of the event and cost $50.

·         Advance tickets purchased 10 or more days before the event cost $30, and advance tickets purchased fewer than 10 days before the event cost $40.

·         Student advance tickets are sold at half the price of normal advance tickets: When they are purchased 10 or more days early they cost $15, and when they are purchased fewer than 10 days early they cost $20.

Implement a class called Ticket that will serve as the superclass for all three types of tickets.  Define all common operations in this class, and specify all differing operations in such a way that every subclass must implement them.  No actual objects of type Ticket will be created: Each actual ticket will be an object of a subclass type.  Define the following operations:

·         The ability to construct a ticket by number.

·         The ability to ask for a ticket’s price.

·         The ability to println a ticket object as a String.  An example String would be “Number: 17, Price: 50.0”.

 

Note that Ticket has one field, ticketNumber. The price of a ticket is determined by the subclass, but all Tickets (Tickets and its subclasses) should have a getPrice() method. That means class Ticket itself needs a getPrice method, but each subclass will override it. You can code getPrice() in Ticket to return -1, or use the “abstract” keyword as shown on pg. 630 to avoid having to code it at all. Note the statement that no objects of class Ticket will be created, so the -1 return from getPrice() will never happen. We’ll get started on this in class.

10.   CH 9, exercises: problem 6, pg. 624.

Implement a class called walkupTicket to represent a walk-up event ticket.  walk-up tickets are also constructed by number, and they have a price of $50.

 

 

 

 

 

Get 20% Discount on This Paper
Pages (550 words)
Approximate price: -

Try it now!

Get 20% Discount on This Paper

We'll send you the first draft for approval by at
Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

Custom Writings Help is a Quality-Oriented Company in Online Writing as a result of hiring exceptional professionals to execute clients' tasks.

Essays

Research Papers

At Custom Writings Help,We understand the struggle of research paper writing, and that is why at Custom WritingS Help, we are all out to help you. We pride ourselves on having a team of clinical writers. The stringent and rigorous vetting process ensures that only the 'BEST' Writers are chosen for the job. We have highly qualified Ph.D. and MA writers working with us; we equally offer these experienced writers specific bonuses and incentives to make them deliver highly original, unique, and informative content at reasonably low prices.

Admissions

Thesis Writing Service

Worlwide, Many Masters Students are struggling with Thesis Completion. A thesis is likely to be the longest and most challenging piece of work a student has ever completed. However, unlike essays and other assignments, a student can pick a particular interest topic and work on their initiative. Fortunately, we are there for you. At Custom Writings Help, you are assured of an authentic, imaginative, informative, linguistically great, and advantageous thesis that adheres to all your needs. So, why continue considering different writers when you have discovered the best in the field?

Editing

Custom Papers

Not a single student can avoid writing custom papers. However, a total lack of experience, skills, and time makes it very hard to produce a superb writing piece. Therefore, if you are seeking professional help, turn to us. Our specialized and experienced writers compose a variety of model papers, including custom essays, college term papers, research papers, book reports, MBA essays, executive summaries, dissertations, Ph.D. theses, admission essays, and research proposals for college and university students at any level.

Coursework

Essay Writing

Most of the students disregard the critical principles of essay writing and compose papers below sensible guidelines. Therefore, with Custom Writings Help, one should not worry about his/her essay. Our Writers compose informative and engaging content on all complexities and topics. We write meaningful and smart essays while prioritizing all aspects that bring about a good grade, such as impeccable grammar, proper structure, zero-plagiarism, and conformance to guidelines.

Coursework

Coourse Work Writing

Don't let the seemingly never-ending onslaught of writing assignments get you down. If you are looking where to get course work assistance online, the writers at Custom Writings Help are here to assist you with all of your writing needs. We undertake to unique delivery of papers that meet the professor's requirements. The content is proofread, edited, and checked plagiarism before submission to customers. No matter how big or small your work is, we will deliver on time. Try US Now! !

Coursework

Dissertation Writing Service

High-Quality Dissertation Writing Services are rare. They require Ph.D. academicians – not easily found. However, are an exception. The years, time, and resources we have invested in the dissertation world has given us a competitive advantage over others. Choose to come to Custom Writings Help; You will find perfect Ph.D. consultants who have written hundreds of dissertations theses ready to help you. Let our dissertation-writing services help you craft your dissertation, for you are assured we will give you the results.

Open chat