Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Programming Language Expert

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

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M92075767
  • Price:- $45

Priced at Now at $45, Verified Solution

Have any Question?


Related Questions in Programming Language

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 - hand execution of arraysoverviewin this task you

Task - Hand Execution of Arrays Overview In this task you will demonstrate how arrays work by hand executing a number of small code snippets. Instructions Watch the Hand Execution with Arrays video, this shows how to ste ...

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

Question 1 what is a computer program what is structured

Question: 1. What is a Computer program? What is structured programming? 2. What is modular programming? Why we use it? 3. Please evaluate Sin (x) by infinite series. Then write an algorithm to implement it with up to th ...

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

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

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

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

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

  • 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