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

I\'m having trouble understanding what I should be inputting to create a \"test

ID: 3751182 • Letter: I

Question

I'm having trouble understanding what I should be inputting to create a "test file" for this counter class. It's part of an assignment I need to resubmit for credit. I've never done a "test file" before, I'm fairly new to Java programming. I'm using NetBeans, and my professor also asked for us to "comment out any lines that begin with 'pacakge'" so he can grade in JGrasp. I've asked him what that means and his answers are misleading and unimformative. The code I have for the counter is as follows: (I just need to create the test file and figure out what commenting out lines that being with package means)..

public class Counter
{
int count;
public Counter()
{
count = 0;
}
public void setcounter(int k)
{
if(k>=0) count =k;
}
public Counter(int k)
{
if(k>=0) count=k;
}
void Increment()
{
count++;
}
void Decrement()
{
if(count!=0)
count--;
else
System.out.println("Error - Attempted to subtract 1 widget from 0 widgets.");
}
@Override
public String toString()
{
return ""+count;
}
public boolean equals(Counter other)
{
return (count==other.count);
}
public void reset()
{
count = 0;
System.out.println("Number of widgets reset to 0.");
}
public static void main(String[] args)
{
System.out.println("This program creates and uses Counters");
System.out.println("Initial state");
Counter counter1 = new Counter();
Counter counter2 = new Counter();
System.out.println("counter1 is at" +counter1);
System.out.println("counter2 is at" +counter2);
System.out.println("counter1" + (counter1.equals(counter2)? "equals":"not equals") + counter2);
counter1.Decrement();
counter1.setcounter(5);
counter2.Increment();
System.out.println("Enter after first test");
System.out.println("counter1 is at "+counter1);
System.out.println("counter2 is at " +counter2);
System.out.println("counter1 " + (counter1.equals(counter2)? "equals":"not equals") + counter2);
System.out.println("Enter after second test");
System.out.println("counter1 is at " +counter1);
System.out.println("counter2 is at " +counter2);
System.out.println("counter1" + (counter1.equals(counter2)? "equals":"not equals") + counter2);
System.out.println("Process completed");
  
}
}

Explanation / Answer

I guess he wants the testing of the class in a different file and just the class code separately. I have separated the main() from the Counter class and created a separate test class TestCounter.java. Check if this is what the instructor needs.
Your code is not using packages. So you don't need to worry about that.


public class Counter
{
int count;
public Counter()
{
count = 0;
}
public void setcounter(int k)
{
if(k>=0) count =k;
}
public Counter(int k)
{
if(k>=0) count=k;
}
void Increment()
{
count++;
}
void Decrement()
{
if(count!=0)
count--;
else
System.out.println("Error - Attempted to subtract 1 widget from 0 widgets.");
}
@Override
public String toString()
{
return ""+count;
}
public boolean equals(Counter other)
{
return (count==other.count);
}
public void reset()
{
count = 0;
System.out.println("Number of widgets reset to 0.");
}

}

TestCounter.java
----------

public class TestCounter{
public static void main(String[] args)
{
System.out.println("This program creates and uses Counters");
System.out.println("Initial state");
Counter counter1 = new Counter();
Counter counter2 = new Counter();
System.out.println("counter1 is at" +counter1);
System.out.println("counter2 is at" +counter2);
System.out.println("counter1" + (counter1.equals(counter2)? "equals":"not equals") + counter2);
counter1.Decrement();
counter1.setcounter(5);
counter2.Increment();
System.out.println("Enter after first test");
System.out.println("counter1 is at "+counter1);
System.out.println("counter2 is at " +counter2);
System.out.println("counter1 " + (counter1.equals(counter2)? "equals":"not equals") + counter2);
System.out.println("Enter after second test");
System.out.println("counter1 is at " +counter1);
System.out.println("counter2 is at " +counter2);
System.out.println("counter1" + (counter1.equals(counter2)? "equals":"not equals") + counter2);
System.out.println("Process completed");
  
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote