Question Details

(solution) CS141 programming II (java) Assignment 1 Chapter 9 -Interface,


the assignment is attached  plz answer all the questions 


CS141 programming II (java) Assignment 1

 

Chapter 9 ?Interface, polymorphism

 

Chapter 10 - Inheritance

 

Assignment 1 worth 5 points (50/10). Due on Sunday 16/10/2016. Will deduct 1

 

point for each day late. Copying each other work will result in giving 0 to both. Q1. Explain why and give examples. ?In OO programming Polymorphism is only

 

possible when using Interface or Inheritance? . (5 points) Q2. Can we create an object of type Interface? Explain your answer. (5 points) Q3. Complete the following code, and run to demonstrate the use of Interface. (10

 

points) The main class tester is given to you, complete the code in the Interface and the two

 

Classes.

 

Notice when coding Interface using Eclipse choose New Interface, NOT New Class

 

Here is a sample Run:

 

Return

 

Return

 

Return

 

Return 0.0

 

2000.5

 

1.0

 

0.25 public class chapt9Tester {

 

of the code // the tester is complete, just run it after you finish the rest public static void main(String args) { // create two bankaccount objects a1 using constructor without parameters, and

 

object a2 using constructor with parameter sending it the amount 2000.50

 

BankAccount a1 = new BankAccount();

 

BankAccount a2 = new BankAccount(2000.50);

 

// create two coin objects c1 using constructor without parameters and c2 using

 

constructor and sending 0.25 and "Quarter"

 

Coin c1 = new Coin();

 

Coin c2 = new Coin(0.25, "Quarter");

 

// now create an array of 4 elements of type Measurable and load it with the 4

 

objects that you just created

 

Measurable marr = { a1, a2, c1, c2 };

 

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

 

double r = marr[i].getMeasure();

 

differently depending on the object that called it // getMeasure() will behave System.out.println("Return " + r);

 

}

 

// notice that a variable of type Measure marr can refer to BankAccount

 

object or Coin object without casting.

 

// however it can only call the methods that are declared in the Measure

 

interface

 

} }

 

// This is the Interface complete the missing code, (if you use Eclipse: click on File -> New ->

 

Interface )

 

public _______________ Measurable {

 

double getMeasure();

 

} // BankAccount class will use the interface

 

public class BankAccount ___________________ Measurable{

 

private double balance;

 

public BankAccount(double amt) { //constructor with parameter balance = amt;

 

}

 

public BankAccount() { //constructor without parameter, set balance to 0

 

____________________

 

}

 

public void deposit(double amt){

 

balance = balance + amt;

 

}

 

public void withdraw(double amt){

 

balance = balance - amt;

 

}

 

@Override

 

public double getMeasure() {

 

return balance;

 

}

 

} // Coin class will use the same interface

 

public class Coin ________________ ____________________ {

 

private double value = 1;

 

private String name = "Dollar"; public Coin(double v, String n){ // constructor with two parameters value = v;

 

name = n;

 

}

 

public _______________ { // constructor without parameters value = 1;

 

_____________________;

 

} // implement the interface method getMeasure here public _________ ___________________ {

 

return value;

 

}

 

} Q4. What is the output of the following code: (10 points)

 

public class InheritanceTester {

 

public static void main(String args) {

 

MySubClass mysub = new MySubClass();

 

System.out.println(mysub.myMethod());

 

}

 

}

 

public class MySubClass extends MySuperClass{

 

public MySubClass()

 

{ called..");

 

} System.out.println("Subclass constructor has been }

 

public class MySuperClass {

 

public MySuperClass()

 

{

 

System.out.println("Superclass constructor has been

 

called..");

 

}

 

public String myMethod()

 

{

 

return "Superclass method has been called";

 

}

 

} Q5. Consider the following problem description and write java code: (20 points) A company need to store its employees information. Each employee has an id,

 

name, date of birth, phone number and salary. Using the diagram, write your java

 

classes that shows the inheritance between these classes, and consider the

 

following:

 

1. Class employee has a method work() which returns the statement ?I am an

 

employee?

 

2. Class employee overrides toString() method to return the employee

 

information. Or if you don?t what to override toString() you can write your

 

own method to return the employee info. (both ways are accepted)

 

3. Each subclass should overrides the method work() to return its work. You

 

need to call work() of the superclass. For example:

 

Method work() in executive class should returns:

 

I am an employee.. I work as an executive. So in each subclass work() method first call it?s

 

parent work() method by using super. Which will

 

return ?I am an employee ..? and then add the child

 

own details. in this example ?I work as an

 

executive?

 

4. Implement the main class Company which constructs objects from the 3

 

subclasses, and test all of their methods.

 

Sample Run will result in:

 

I am an employee.. I work as an executive.

 

My ID is: 1234567890

 

My Name is: John Smith

 

My Date of Birth is: 1-1-1981

 

My phone is: 555-555-12345

 

My Salary is: 12,000

 

I am an employee.. I work as a Software Engineer.

 

My ID is: 0987654321

 

My Name is: Jane Doe

 

My Date of Birth is: 1-1-1988

 

My phone is: 555-555-12345

 

My Salary is: 10,000

 

I am an employee.. I work as a Software Manager.

 

My ID is: 9988776655

 

My Name is: Joe Green

 

My Date of Birth is: 1-1-1978

 

My phone is: 555-555-12345

 

My Salary is: 11,000

 


Solution details:
STATUS
Answered
QUALITY
Approved
ANSWER RATING

This question was answered on: Jan 30, 2021

PRICE: $15

Solution~0001001065.zip (25.37 KB)

Buy this answer for only: $15

This attachment is locked

We have a ready expert answer for this paper which you can use for in-depth understanding, research editing or paraphrasing. You can buy it or order for a fresh, original and plagiarism-free solution (Deadline assured. Flexible pricing. TurnItIn Report provided)

Pay using PayPal (No PayPal account Required) or your credit card . All your purchases are securely protected by .
SiteLock

About this Question

STATUS

Answered

QUALITY

Approved

DATE ANSWERED

Jan 30, 2021

EXPERT

Tutor

ANSWER RATING

GET INSTANT HELP/h4>

We have top-notch tutors who can do your essay/homework for you at a reasonable cost and then you can simply use that essay as a template to build your own arguments.

You can also use these solutions:

  • As a reference for in-depth understanding of the subject.
  • As a source of ideas / reasoning for your own research (if properly referenced)
  • For editing and paraphrasing (check your institution's definition of plagiarism and recommended paraphrase).
This we believe is a better way of understanding a problem and makes use of the efficiency of time of the student.

NEW ASSIGNMENT HELP?

Order New Solution. Quick Turnaround

Click on the button below in order to Order for a New, Original and High-Quality Essay Solutions. New orders are original solutions and precise to your writing instruction requirements. Place a New Order using the button below.

WE GUARANTEE, THAT YOUR PAPER WILL BE WRITTEN FROM SCRATCH AND WITHIN A DEADLINE.

Order Now