Question: PROBLEM CycleThrowTryCatch - Write in Java with comments Revisit the Cycle class in Unit 3.10.1. Modify your application such that the properties, numberOfWheels and weight
PROBLEM CycleThrowTryCatch - Write in Java with comments
Revisit the Cycle class in Unit 3.10.1. Modify your application such that the properties, numberOfWheels and weight are entered as double values interactively (at the keyboard). Exception handling will be used to determine whether a type mismatch occurs.
Edit your application such that, in addition to [A], the values for numberOfWheels and weight, entered interactively, will throw a new exception Values cannot be less than or equal to zero only If the values are less than or equal to zero. Add or use the appropriate try and/or catch blocks.
Directions
Examine your application for the class called Cycle.
Add Try and Catch blocks appropriately.
Add the throw statement for the new exception.
Display an appropriate message if an exception occurs.
Display the properties of the object.
Note: Verify the contents were written to the text file using notepad (or any word processor).
Grading Rubric
| Task | Points |
| Try and Catch blocks added correctly | 1 |
| Appropriate message used for each exception | 1 |
| Throw statements used correctly | 1 |
| Object properties displayed correctly | 1 |
| Proper documentation | 1 |
| Program works effectively | 1 |
| Total | 6 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here is the existing cycle class:
Cycle.java
public class Cycle{
int numberOfWheels; int weight;
public Cycle(){
this(100,1000); }
public Cycle(int numberOfWheels,int weight){
this.numberOfWheels=numberOfWheels; this.weight=weight; }
public String toString(){ return "Number of Wheels: "+numberOfWheels+" Weight: "+weight; } }
=========================================
CycleTest.java
// testing the Cycle Class public class CycleTest { //main method public static void main(String args[]) { //creating the Cycle object with some values Cycle ob=new Cycle(4,100); //printing the values of the object using toString() method System.out.println(ob); } }
==========================
CycleTest2.java
// testing the Cycle Class - default constructor public class CycleTest2 { //main method public static void main(String args[]) { //creating the Cycle class object Cycle ob2=new Cycle(); //printing the values of the object using toString() method System.out.println(ob2); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
