Zach Armstrong Zach Armstrong - 2 months ago 6
Java Question

How do I change a Variable, in a Java Class

I have to be able to convert some variables in my class. I have a boolean variable, WaGa (Stands for Workstation/Gaming computer), and if it's true, I want to convert String WorGam

I have to do this through service and support methods, and I keep trying, but I constenly fail. It just prints out what's in the driver. HELP.

public class Graphics
//instance data
{
private int Ram;
private String Brand;
private int Res;
private int BiWi;
private int BaCl;
private boolean K4;
private boolean WaGa;
private String WorGam;

//boolean WaGa, boolean K4, int BaCl, int BiWi, int Res, String Brand, int Ram


public Graphics (int R, String B, int Re, int Bi, int Ba, boolean K4, boolean Wa, String Wor ) // constructor
{
Ram = R;
Brand = B;
Res = Re;
BiWi = Bi;
BaCl = Ba;
K4 = K4;
WaGa = Wa;
Wor = WorGam;
}

public int get_Ram() //Accessor Method - there are 3 of them
{
return Ram;
}

public String get_Brand() //Accessor Method - there are 3 of them
{
return Brand;
}

public int get_Res() //Accessor Method - there are 3 of them
{
return Res;
}

public int get_BiWi() //Accessor Method - there are 3 of them
{
return BiWi;
}

public int get_BaCl()
{
return BaCl;
}

public boolean get_K4()
{
return K4;
}

public String WorGam(boolean WaGa)
{
String WorGam;
if ( WaGa == true) {
return WorGam = "Workstation";
} else {
return WorGam = "True";
}
}

public String toString()
{
return ("Ram" + " " + Ram + ". " + "Brand:" + " " + Brand + ". " + "Resolution" + " " + Res + ". " + "Processer" + " " + BiWi + "." + " " + "Base Clock" + " " + BaCl+ " " + "K4?" + " " + K4+ " " +WorGam);
}
}

public class Graphicse_Driver
{
public static void main(String [] args)
{

Graphics unique=new Graphics(4, "Nvinda", 6, 7, 9, false, false, "sdf" );

System.out.println(unique);

Answer

You may need to reread you code to make sure there aren't any other mistakes in your code, but this is the root of your problem.

In order to access the WarGam getter, you need to call:

System.out.println(unique.WarGam());

When you do System.out.println(unique), you are trying to print out the entire Graphics object instead of just the WarGam string.

You then should change your WarGam() method to look like the following:

public String WorGam()
{
    if (WaGa) {
        return "Workstation";
    }

   return "Gaming";
}

Here is a more in depth explanation of the changes:

  1. WaGa is a private variable of your Graphics class. Since the WarGam() method is in the same Graphics class, it already had access to the WaGa variable, so you do not need to pass it in.
  2. if(WaGa == true) is just a wordier way of writing if(WaGa).
  3. Instead of creating a String WorGam variable, you can just return the string you want directly.
  4. The else surrounding the second return is unnessary since that code will only be hit if the first return is skipped.

After these changes, the private String WarGam variable is really not necessary either.