-
Testing a method
I have the following code and need to test my methods in class Violin. I only keep getting one line of output. I'm trying to test each element in the violins [] array and get output for each. This is for my first java programming class so I will apologize upfront for asking elementary questions.
Thanks
- Cory
Code:
package corybrownp3;
public class Corybrownp3 {
public static void main(String[] args) throws Exception{
//declare violin object
Violin violin1 = new Violin();
java.io.File file = new java.io.File("Corybrownp3test.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);
//write test results to file
output.println(violin1.playViolin());
output.println(violin1.stopPlaying());
output.println(violin1.tuneViolin());
if (Violin.isTuned && Violin.isPlaying)
System.out.println("The violin is tuned and playing!");
else
System.out.println("The violin is not playing, or it is not tuned.");
}
}
class Violin
{
static boolean isTuned; //Violin starts off not tuned
static boolean isPlaying; //Violin is not playing at start
//array for Violin strings
char [] violinStrings = {'E','A','D','G'};
//default Violin object
public Violin()
{
isTuned = false;
isPlaying = false;
//System.out.println("The violin is not playing, and it is not tuned.");
}
public Violin(boolean T, boolean P)
{
isTuned = T;
isPlaying = P;
}
public boolean playViolin()
{
//System.out.println("The violin is playing!");
return isPlaying = true;
}
public boolean stopPlaying()
{
//System.out.println("The violin has stopped playing.");
return isPlaying = false;
}
public boolean tuneViolin()
{
//System.out.println("The violin is being tuned!");
return isTuned = true;
}
public class ViloinTest{
public void main(String[] args){
Violin[] violins;
violins = new Violin[10];
for(int i = 0; i < violins.length; i++){
violins[i] = new Violin();
}
//Test Violin methods
for (int i = 0; i < violins.length; i++)
System.out.println(Violin.isPlaying + " ");
}
}
}
-
Re: Testing a method
Don't forget to close your PrintWriter object, output, after you're done writing to it.
-
Re: Testing a method
-
Re: Testing a method
Quote:
Originally Posted by
DarrylBurke
Thanks, db.
-
Re: Testing a method
Any takers on helping me out with this?
-
Re: Testing a method
Your code looks good, except you need get rid of public from "public class ViloinTest", since there is only allowed PUBLIC class in a package.
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package corybrownp3;
public class Corybrownp3 {
public static void main(String[] args) throws Exception{
//declare violin object
Violin violin1 = new Violin();
java.io.File file = new java.io.File("Corybrownp3test.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);
//write test results to file
output.println(violin1.playViolin());
output.println(violin1.stopPlaying());
output.println(violin1.tuneViolin());
if (Violin.isTuned && Violin.isPlaying)
System.out.println("The violin is tuned and playing!");
else
System.out.println("The violin is not playing, or it is not tuned.");
}
}
class Violin
{
static boolean isTuned; //Violin starts off not tuned
static boolean isPlaying; //Violin is not playing at start
//array for Violin strings
char [] violinStrings = {'E','A','D','G'};
//default Violin object
public Violin()
{
isTuned = false;
isPlaying = false;
//System.out.println("The violin is not playing, and it is not tuned.");
}
public Violin(boolean T, boolean P)
{
isTuned = T;
isPlaying = P;
}
public boolean playViolin()
{
//System.out.println("The violin is playing!");
return isPlaying = true;
}
public boolean stopPlaying()
{
//System.out.println("The violin has stopped playing.");
return isPlaying = false;
}
public boolean tuneViolin()
{
//System.out.println("The violin is being tuned!");
return isTuned = true;
}
}
class ViloinTest{
public void main(String[] args){
Violin[] violins;
violins = new Violin[10];
for(int i = 0; i < violins.length; i++){
violins[i] = new Violin();
}
//Test Violin methods
for (int i = 0; i < violins.length; i++)
System.out.println(Violin.isPlaying + " ");
}
}
The violin is not playing, or it is not tuned.
成功生成(总时间:6 秒)
Ksharp