What is code reuse

Assignment Help Programming Languages
Reference no: EM13763454

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:
https://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.

Reference no: EM13763454

Questions Cloud

Limitations of sutherland approach : 1. What are some of the principal elements of E.H. Sutherland's contribution to the study of white collar crime? 2. What are some of the limitations of Sutherland's approach
How widesperead is the use if of this drug explain : How widesperead is the use if of this drug/ Explain and What type of demographics or population are most likely to use this drug
Search for policy examples on the internet : Outsourcing generally, and offshore outsourcing in particular, continues to be a key part of many companies' supply and cost management strategy. The strategy has proven to be effective but brings with it significant risks that must be recognized ..
Differences between the risk and the protective factors : Explain the key differences between the risk factors and the protective factors that can exist in juvenile delinquency among males and females
What is code reuse : What is code reuse. How does inheritance help achieve code reuse
Compare and contrast with the policy it replaces : Compare and contrast with the policy it replaces and Articulate your ideas on how to remedy the issue of corporate malfeasance
Ethical theories-consequentialism and deontology : Incorporate normative ethics, metaethics, and applied ethics, and discuss one or more the following modern ethical theories: consequentialism, deontology, and virtue theories, taken from utilitarianism, Kantianism, and Aristotelianism
Benefits or roi of social media : Prepare and submit a 1,050- to 1,400-word or 4- to 5-page analysis of the expected costs or investment and benefits or ROI of social media hosted by the researched organization to interact with customers and prospects, stakeholders, or their serve..
What would you like to know more about city : What would you like to know more about? What would you like to make sure other people know about?

Reviews

Write a Review

Programming Languages Questions & Answers

  Create modular program to enter monthly costs

Create modular program which ask user to enter monthly costs for the following expenses incurred from operating his or her automobile: loan payment, insurance, gas, oil, tires, and maintenance.

  Program to display total sales salesmen

Display total sales per (6) salesmen. Place first and last names for (6) salesmen in array. When you display your final output, print salesman's last name only.

  Design and develop a unix file sharing system

Design and develop a UNIX file sharing system. For this task you will be required to develop a simple application in C programming language.

  Based on the keston 2013 article in the electronic reserve

write a 200- to 300-word short-answer response to the followingbased on the keston 2013 article in the electronic

  Create program by using pseudocode along with structure

Create a program by using pseudocode along with structure to do the following. Permit the user to enter series of temperatures in degrees Celsius (C) terminated by -999.

  Program that satisfies requirements of programming project

Write a program that satisfies the requirements of Programming Project 2 of Hanly, and that uses the following modifications and tips -Use the COLOR_CODES string array.

  Create logic for program-continously prompts for numeric

Create the logic for a program that continously prompts the user for two numeric values that represent the sides of a rectangle.

  Write a program that exploit string library functions

Write a program that exploit string library functions to do the following: -prompt the user to enter his name (of four parts) -count the number of characters in his name (excluding the white spaces).

  Implement sumpairs and sumof

Using SML writes a version of sumPairs that sums each component of the pairs separately, returning a pair consisting of the sumof the first components and the sum of the second components

  Write program to prints question do you want to continue

Write a program which prints question"do you want to continue?"and reads user input. if user input is"y", "yes", "ok", "sure", or "why not?" , print out "ok".

  Distinguish class templates and program with heading

Clearly distinguish each class templates and their program with heading. Elaborate each step and give it without errors. Develop classes or class templates for the following.

  How can you use a hash function

How can you use a hash function to evaluate duplicate files (even when the file name is changed) - Give the answer of given question and also provide details.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd