What is inheritance and why would we want to use itwhen


Assignment

Question 1

Consider the following class. The purpose of each block of code within the class is written in comments within the code. But the following class contains a number of errors.

Errors can occur at compilation time; or at runtime; and others are logic errors (the hardest to find as the program will both compile and run but not produce the expected result).

• Fix all the errors so the code will compile, run and produce the expected outcome. For each error you should do the following:

a) List each error
b) For each error, state what type of error it is
c) Rewrite the code so that each error is fixed and the program would run as expected.

public class FixTheErrors {

String phrase = "Programming is cool!"; int aNumber = 4;
boolean found == false;
double[] someNumbers = {5.4, 17, 65, 1, 95.2};

// set the value of an instance variable

FixTheErrors(String aPhrase) { aPhrase = phrase;
}

// check whether or not the boolean is false and if it is
// times the instance variable by the received value
// then return the final value of the variable passed in

int calculateTimesResult(int timesByValue) { if (found = false) {
return timesByValue *= aNumber;
} else {
timesByValue = 0;
}
}

// add all the numbers in the array and print the
// addition total each time through the loop

addTheNumbers() {
for (int i = 1; i <= someNumbers.length; i++) { double answer = 0;
answer += someNumbers[i]; System.out.println(answer);
}

}

}

Question 2

There are two Java classes: SuperClass and SubClass. These classes are listed below along with the main method of the main class.

a) In the main method, what result would each of the print statements produce?
b) Explain why each method call to the doSomething methods returns its unique result.

SuperClass

public class SuperClass {
public int doSomething(int a, int b) { return a * b;
}
}

SubClass

class SubClass extends SuperClass {  public int doSomething(int a, int b) {
return super.doSomething(a, b) + a + b;
}
}

The Main method

public static void main(String args[]) {
SuperClass sup = new SuperClass(); System.out.println("sup's result is:" +
sup.doSomething(4, 3));
SubClass sub = new SubClass(); System.out.println("sub's result is:" +
sub.doSomething(4,3));
}

Question 3

Local variables don't have modifiers assigned to them. Explain why this is so.

Question 4

Consider a class named "PlayingCard". It exists to manage information about a playing card during a card game. Playing cards have suits: the choices are: "Hearts", "Diamonds", "Spades" or "Clubs". And each card has a value: the choices are: 2 - 10, Ace, King, Queen or Jack. In this game there are 52 of these playing cards used and each is unique.

• Write the code that will create a PlayingCard class with the following data and methods:

a) A card suit
b) A card value
c) A default constructor
d) An overloaded constructor that allows the card's suit and value to be set at the start of the game.

Note:

e) A way to find out separately what suit and value a particular playing card has.

You are not asked to write the whole program. Just create the PlayingCard class.

Question 5

This question is about methods in Java.

a) What does it mean for a method to return a value to a program?

b) If a method has two String data types in its parameter list can it return a boolean value to the program? Explain your answer.

c) In what circumstances would you declare a method to be ‘private'?

d) Is the following statement true or false? Justify your answer.

o "An instance variable can be used inside a static method".

Question 6

Write a method that will do the following:

a) Receive three values (two names and a numeric amount)
b) Check whether the two names are the same and the numeric amount is greater than 13
c) If neither of the above is correct the method will return "Neither is true".
d) If both are correct the method will return "Both are true".
e) Otherwise it should return "Only one of them is true".
f) Your method should be called "compareData"

Question 7

a) Write an ArrayList that will hold the following elements:

o "encapsulation", "inheritance", "instantiation", "data", "object-oriented"

b) Write the code that will loop through the ArrayList checking each word as you go.

a. If a word contains the letter ‘i' print the following to the console:

"An ‘i' has been found!"

NOTE:

Be careful when deciding what data type the elements are.

Do not use the contains method of the String class. Use a loop.

Question 8

Java offers the capability to both overload and override methods.

You have two methods that are both named calculate in the same class and these both return a double value. The first method receives a single value through its parameter list. The second method receives two values through its parameter list.

a) Would we say that the calculate method is overloaded or overridden?
b) Justify your answer.

Question 9

a) What is inheritance and why would we want to use it?
b) When using inheritance, does a subclass inherit both variables and methods of the super class?
c) Justify your answer to part b)

Question 10

There are three classes listed below: A, B and the Test class which holds the main method.

a) What is the output of the following program when it runs?
b) Explain in detail how you arrive at the answer.

public class A {
public int i; public int j;
public A() {
}
public int getI() { return i;
}
public int getJ() { return j;
}
}

public class B extends A {
public B(int i, int j) { this.i = i;
this.j = j;
}
void display() {
super.j = super.i +3; System.out.println(super.i + " " + super.j);
}
}

public class Test {
public static void main(String[] args) { B obj = new B(4, -2);
obj.i = 1;
obj.j = 2;
obj.display();
}
}

Solution Preview :

Prepared by a verified Expert
Programming Languages: What is inheritance and why would we want to use itwhen
Reference No:- TGS01723819

Now Priced at $45 (50% Discount)

Recommended (97%)

Rated (4.9/5)