Part A

Given the following declarations, write down the output of this code:

class SomeBadCode { //This is what happens when you don't indent or name your variables properly
public static int a = 0;
public int b = 1;
private int c = 2;

public SomeBadCode() {
b = 0;
c = 0;
a++;
}

public SomeBadCode(int b, intc) { // This is what happens when you don't use the 'this' keyword
b += b;
c += c;
a++;
a += b + c;
}

public static void main(String[] args) {
SomeBadCode a = new SomeBadCode();
System.out.println(a.a); //(Q1)
System.out.println(a.b); //(Q2)
System.out.println(a.c); //(Q3)
SomeBadCode b = new SomeBadCode(7,11);
System.out.println(b.a); //(Q4)
System.out.println(b.b); //(Q5)
System.out.println(b.c); //(Q6)
SomeBadCode c = new SomeBadCode(12,13);
System.out.println(c.a); //(Q7)
System.out.println(c.b); //(Q8)
System.out.println(c.c); //(Q9)
System.out.println(SomeBadCode.a); //(Q10)
}
}

Part B

The source code below does not compile.

First, explain the problem then write the code to fix it.

The contents of Main.java file:

class Main { //Line1
public static void main(String[] args){ //Line2
String inputString = JOptionPane.showInputDialog("Please enter a string"); //Line 3
System.out.println("This is what you have entered: " + inputString); //Line 4
} //Line 5
} //Line 6

Part C

The code below compile and runs. However, for some reason, it goes into infinite loop even though user entered the exact string "you are beautiful" (ignore double quotations); Explain the problem and provide the fix for this code.

Contents of AnotherBadCode.java file:

import javax.swing.*; //Line 1
class AnotherBadCode{ //Line 2
public static void main(String[] args) { //Line 3
String inputString = JOptionPane.showInputDialog("Please enter something"); //Line 4
while(inputString != "you are beautiful") { //Line 5
inputString = JOptionPane.showInputDialog("Please enter something"); //Line 6
} //Line 7
} //Line 8
} //Line 9

Part D

The code below compile and runs. However, your IT teammates complained that each circle object taking up more than 80 bytes of space than it should. (Assume that a double takes 8 bytes of space in memory hypothetically)

Contents of Circle.java file:

import javax.swing.*; //Line 1
class Circle { //Line 2
public static int count = 0; //Line 3
private double radius = 1; //Line 4
//You can safely assume that this is all the circle radius value that user wants to create
private double[] radiusRange = {1,2,3,4,5,6,7,8,9,10}; //Line 5
public Circle() { //Line 6
radius = radiusRange[(int)(Math.random()*radiusRange.length)]; //Line 7
count++; //Line 8
} //Line 9
public static void main(String[] args) { //Line 10
String inputString = JOptionPane.showInputDialog("Please enter the number of circle(s) to create:"); //Line 11
int magicNumber = Integer.parseInt(inputString); //Line 12
MickeyMouse(magicNumber); //Line 13
System.out.println("Total circles created is: " + Circle.count); //Line 14
} // Line 15
// The last programmer named this method after his/her favorite Disney Character
public static void MickeyMouse(int magicNumber) {
if(magicNumber == 0)
return;
Circle temp = new Circle();
for(int i = 0; i < magicNumber; i++) {
MickeyMouse(magicNumber - 1);
}
}}

Part D1:

Explain in one sentence or two why or what the problem could be (based on the code above.)

Part D2:

Write the code that will fix the problem: (indicate which lines of code you are changing)

Part D3:

User wants to know why and how your program output:

Total circles created is: 6235301

When user entered 10 at the JOptionPane popup dialogbox.

You can draw a diagram to explain to me what's going on.

(Assume that user never enters a non-integer for this problem)

Part E

Write a Method called CheckForValidMoney in which it will

  • Accept a parameter of string type called inputString
  • Returns a Boolean value
  • Check that the inputString is actually a double. (Use Double.parseDouble()) Return false if it is not a number that can be parsed into a double.
  • If it is a double, check that it is not less than or equal to ZERO. [NOT(radius <= 0)]
  • If it is zero or negative in value, make sure that your method return false.
  • There should only be ONE return statement in your method.

Part F

Write a custom Exception class called InvalidMoneyException in which it will inherit from the Exception class.

Provide three constructors:

  • The first constructor should be the default constructor and accept no parameters. Inside this constructor, output the message: "Invalid money value was given."
  • The second constructor should accept a string parameter called customMessage. Inside this constructor, output the given customMessage.
  • The third constructor should accept a double parameter type called moneyValue. Output the message:
"Invalid money parameter was given. Try to create money with the value < moneyValue>"
  • (Replace < moneyValue> with the actual value of money)

Part G

Modify your method from Part E to now throw the new custom exception object from the class in part F that you have created if user now tries to create an invalid Money object. (0 or negative in value)

Just indicate which lines of code you will have to change to throw your custom exception object:

Extra Credit

Using what you have learned in class, Design 4 classes:

  • DollarBill
  • OneDollar
  • TwentyDollar
  • HundredDollar

Decide which class should inherit from with class, what attributes they should all have especially your static or non-static members. You can either draw the boxes or write the actual code. Either way, your design has to be very clear. (For e.g., if you make me search for it, you will lose points...)

[You don't have to provide the complete code. But if it will help you, you can provide the code. Typically, if you code before designing it, you will end up with a disaster.]

Indicate which attributes are static, which are non-static members in your design. Which attributes should be inherited and which should not? Which attributes that is shared by all and which cannot be shared? Which attribute belongs specifically to the actual object itself and not the class?

(Remember to use final if you want to use a constant or it will not count)

* Provide sample code for the constructors and also the code to (print money) instantiate the object of your class.

For e.g., can a user print money(instantitate your object) using this code: DollarBill a = new DollarBill() be used? Why and Why not? Does it make sense? Provide your assumptions.

Other things to think about, if user wants to know how much is TwentyDollar worth, does it make sense that you need to print the dollar (instantiate the object) before the user can know its worth?

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.