how to add an interactive method to change some attributes? * * To change this t
ID: 3647546 • Letter: H
Question
how to add an interactive method to change some attributes?*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package instrument;
import java.io.FileNotFoundException;
/**
*
* @author Owner
*/
public class Instrument {
public static void main(String[] args) throws FileNotFoundException {
java.io.File file = new java.io.File("Sybil.txt");
//check to see if file already exists
if (file.exists())
{
System.out.println("File already exists");
System.exit(0);
}
//create a file
java.io.PrintWriter output = new java.io.PrintWriter(file);
//declare cello object
Cello Cello1 = new Cello();
//write test results to file
output.println(Cello1.playCello());
output.println(Cello1.stopPlaying());
output.println(Cello1.tuneCello());
}
}
class Cello
{
boolean isTuned;
boolean isPlaying;
char [] CelloStrings = {'E','A','D','G'};
public Cello()
{
isTuned = false;
isPlaying = false;
System.out.println("The Cello is not playing, and it is not tuned.");
}
public Cello(boolean T, boolean P)
{
isTuned = T;
isPlaying = P;
}
public boolean playCello()
{
System.out.println("The Cello is playing!");
return isPlaying = true;
}
public boolean stopPlaying()
{
System.out.println("The Cello has stopped playing.");
return isPlaying = false;
}
public boolean tuneCello()
{
System.out.println("The Cello is being tuned!");
return isTuned = true;
}
}
Explanation / Answer
please rate - thanks
hope this is what you're looking for
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package myinstrument;
import java.util.*;
import java.io.FileNotFoundException;
/**
*
* @author Owner
*/
public class MyInstrument {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException {
java.io.File file = new java.io.File(".txt");
//check to see if file already exists
if (file.exists())
{
System.out.println("File already exists");
System.exit(0);
}
//create a file
java.io.PrintWriter output = new java.io.PrintWriter(file);
//declare cello object
Cello Cello1 = new Cello();
//write test results to file
output.println(Cello1.playCello());
output.println(Cello1.stopPlaying());
output.println(Cello1.tuneCello());
Scanner in=new Scanner(System.in);
int option;
boolean notDone=true;
while(notDone)
{System.out.println(" What would you like to do? ");
System.out.println("1-tune the cello");
System.out.println("2-play the cello");
System.out.println("3-stop playing the cello");
System.out.println("4-stop the program");
option=in.nextInt();
if(option==1)
Cello1.tuneCello();
else if(option==2)
Cello1.playCello();
else if(option==3)
Cello1.stopPlaying();
else if(option==4)
notDone=false;
else
System.out.println("Invalid option");
}
}
}
class Cello
{
boolean isTuned;
boolean isPlaying;
char [] CelloStrings = {'E','A','D','G'};
public Cello()
{
isTuned = false;
isPlaying = false;
System.out.println("The Cello is not playing, and it is not tuned.");
}
public Cello(boolean T, boolean P)
{
isTuned = T;
isPlaying = P;
}
public boolean playCello()
{
System.out.println("The Cello is playing!");
return isPlaying = true;
}
public boolean stopPlaying()
{
System.out.println("The Cello has stopped playing.");
return isPlaying = false;
}
public boolean tuneCello()
{
System.out.println("The Cello is being tuned!");
return isTuned = true;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.