Ask Programming Language Expert

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

2. 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. 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. Explain the difference between the this keyword and the super keyword. When should each be used?

5. 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. 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. 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. 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. 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. 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.

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M91333226
  • Price:- $50

Guranteed 36 Hours Delivery, In Price:- $50

Have any Question?


Related Questions in Programming Language

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

Assignment task -q1 a the fibonacci numbers are the numbers

Assignment Task - Q1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As