Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming CycleFileOutput - Write in Java with comments Revisit the Cycle clas

ID: 3919082 • Letter: P

Question

Programming CycleFileOutput - Write in Java with comments

Revisit the Cycle class in Unit 3. Modify your application such that the properties will be written to a text file called “CycleOut.txt” instead of to the screen.

Directions

Examine your application for the class called Cycle.

Add an appropriate throws statement in the main method.

Create a reference to a File class with the appropriate name of a text file (Cycle.txt).

Use appropriate code to ensure that the text file exist.

Output the values of the variables to the text file.

Close the file.

Note: Verify the contents were written to the text file using notepad (or any word processor).

Grading Rubric

Task

Points

Throws clause added in main method

1

Create a reference to the File class and text file

1

Check whether the text file exists

1

Output the properties to the text file

1

Close the text file

1

Proper documentation

1

Program works effectively

1

Total

7

=========================================

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

Task

Points

Throws clause added in main method

1

Create a reference to the File class and text file

1

Check whether the text file exists

1

Output the properties to the text file

1

Close the text file

1

Proper documentation

1

Program works effectively

1

Total

7

Explanation / Answer

Here is the completed code for this problem. Modified the CycleTest class to save the output into a file. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need to change anything. Thanks (No changes made to Cycle class)

// CycleTest.java

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

//testing the Cycle Class

public class CycleTest {

                // main method

                public static void main(String args[]) throws IOException {

                                // creating the Cycle object with some values

                                Cycle cycle1 = new Cycle(4, 100);

                                // creating a File object to open CycleOut.txt

                                File cycleFile = new File("CycleOut.txt");

                                // checking if the file exists

                                if (!cycleFile.exists()) {

                                                // file does not exist

                                                System.out.println(cycleFile

                                                                                + " does not exist, will create a new one.");

                                }

                                // Creating a PrintWriter object using the file, this will create new

                                // file

                                // if not exists, otherwise overwrite the existing one.

                                PrintWriter writer = new PrintWriter(cycleFile);

                                // writing to the file, contents of cycle object

                                writer.println(cycle1.toString());

                                // closing the file

                                writer.close();

                                System.out.println("Details are saved in " + cycleFile);

                }

}

/*OUTPUT*/

CycleOut.txt does not exist, will create a new one.

Details are saved in CycleOut.txt

/*CycleOut.txt*/

Number of Wheels: 4

Weight: 100