New to java and need some help. I am working with 2 files. One defines a class and all of it's methods and another constructs an instance of this class. My project requires that we write the output from the class methods to a file. My question is how do I get the methods in my "class definition" file to write to the same file as that defined in my "main method"? I am currently using System.out.print which I know is not correct.
//File #1
public class JessiahP3 {
boolean isPlaying = false;
int strings = 1;
boolean isTuned = false;
public String instrumentName;
//is tuned
public void tune() {
if(isTuned == false)
isTuned = true;
else
isTuned = false;
}
//instrument name
public void setInstrumentName(String insName){
instrumentName = insName;
}
//get instrument name
public String getInstrumentName(){
return instrumentName;
}
private Boolean getTuned(){
return isTuned;
}
public void playInstrument(){
isPlaying = true;
System.out.println("The instrument is now playing.");
}
public void stopInstrument( ){
isPlaying = false;
System.out.print("The instrument has stopped playing.");
}
public void setString(int newString){
if (newString >= 1 && newString <= 6)
strings = newString;
else
System.out.print("You have exeeded the maximum number of strings.");
}
public void stringUp(){
if (strings < 10)
strings++;
}
public void stringdown(){
if (strings > 1)
strings--;
}
public String[] getStringNames(String[] stringNames) {
for (String i:stringNames){
System.out.println(i);
}
return stringNames;
}
}
File #2
import java.util.*;
import java.io.*;
public class JessiahP3Test {
public static void main(String[] args)throws Exception {
//declare new input/ output file
java.io.File newFile = new java.io.File("Jessiahp4.txt");
//java.io.File newFile = new java.io.File(args[0]);
//check to see if file exist
if (newFile.exists()) {
//delete existing file
newFile.delete();
}
//create a file
java.io.PrintWriter output = new java.io.PrintWriter(newFile);
//instance 1 of Instrument = Guitar
JessiahP3 guitar = new JessiahP3();
guitar.setInstrumentName("Guitar");
guitar.setString(3);
output.println("Your current string number is " + guitar.strings);
String[] stringNames = {"Abe", "Blue", "Dream"};
guitar.getStringNames(stringNames);
output.print("Tuning " + guitar.getInstrumentName());
guitar.tune();
guitar.playInstrument();
//output.print("The" + guitar.getInstrumentName() + "is playing");
guitar.stopInstrument();
//close i/o file
output.close();
}
}