What is inheritance and why would we want to use it

Assignment Help Programming Languages
Reference no: EM131321166

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();
}
}

Reference no: EM131321166

Questions Cloud

How is a log linear analysis used : Give an example of when simultaneous, hierarchical, and stepwise multiple regression analyses might be used in research.
Magnitude and direction of the magnetic field in the region : As shown in the figure, a small particle of charge q= -7.0 x 10-6C and mass m= 3.1 x 10-12kg has velocity vo= 11.2 x 103m/s as it enters a region of uniform magnetic field. The particle is observed to travel in the semicircular path shown, with rad..
Estimate carefully the value of charlotte mill to greenearth : Estimate Green Earth's valuation for Charlotte Mill-Estimate carefully the value of the Charlotte mill to GreenEarth. From a financial standpoint, at which price would the purchase be a value-making proposition for GreenEarth
Bucket of concrete is suspended : An 80.0 kg bucket of concrete is suspended from the top rung of the ladder, right next to the wall, as shown in the figure. What is the magnitude of the friction force that the floor exerts on the ladder?
What is inheritance and why would we want to use it : What is inheritance and why would we want to use it? When using inheritance, does a subclass inherit both variables and methods of the super class? Justify your answer to part b)
What is the gold diffusivity in si at 300k : What is the time to failure because the substitutional gold atoms jump (a diffusion step) to adjacent interstitial sites 5A away and Interstitial gold is not an electronic trap.
Is now the time to invest in long term bonds : Define the agency problem, and discuss how to resolve it from the perspective of a stockholder. Is now the time to invest in long term bonds? If not, why not? Has Starbucks breached its fiduciary duty to shareholders by investing its profits in free ..
Describe the most common descriptive statistics : What is the difference between descriptive and inferential statistics?
Calculate the post tax mortgage cost for year : A family currently live in an apartment whose monthly rent is $950. They are thinking of buying a house which would cost $220,000. They plan to live in this house for 5 years and sell it at the end of the 5th year. Calculate the Post tax Mortgage Cos..

Reviews

Write a Review

Programming Languages Questions & Answers

  Modify the program to enter any amount of numbers

Modify the program in Exercise 2a so that the user can enter any amount of numbers up to 12 until a sentinel value is entered.

  Utilize branching and looping syntax with parameters

the file"values.txt" with following data: 88 85 89 64 78 85 92 60 91 96 63 59 83 89 74 93 92 92 63 100. write down the program which will give the following output.

  Create new method public to create and return new picture

Create a new method public Picture frame (int frameSize, Color frameColor) this method will create and return a new picture object.

  Write program to decodes messages using caesar cypher

Write a program which decodes messages. Messages will be encoded using Caesar cypher, with each letter being decoded by replacing it with the previous letter in the alphabet.

  Write a report on t linux kernel programming

Write a report on  t Linux Kernel programming.  Giving a brief introduction about Linux in general, then give in details information about Linux Kernel Programming.

  Develop a concentration game

Concentration, also known as Shinkei-suijaku, Memory, Pelmanism, Pexeso or simply Pairs, is a card game in which all of the cards are laid face down on a surface and two cards are flipped face up over each turn.

  Design a real time flood monitoring system

Assignment 3: Design a Real Time Flood Monitoring System based on Arduino Microcontroller. The system should monitor flood related information like Water level and Precipitation. The data collected from the sensors should be transferred to a client a..

  Event-driven programming or command line programming

Imagine that you have a choice between using event-driven programming or command line programming to develop a computer program. Determine one (1) advantage and (1) disadvantage of using each. Select the style of programming that you prefer, and j..

  Identify some array used in organization

Identify some array used in your organization or an organization for which you know, the business applications; and share the information.

  The set methods should verify that length and width are

rectangle class create a class rectangle with attributes length and width each of which defaults to 1. provide methods

  Write a function to generate lsquorock lsquopaper or

write a function to generate lsquorock lsquopaper or lsquoscissors each answer being equally likely. write a script

  Write advantages of external css and internal css

Write down the advantages of external CSS compared to internal CSS? Describe conditions in which each is suitable type.

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