L
Luch
I'm doing an assignment for a Java class. Basically, we have to build
constructors for a class, Television, whose attributes are *brand* and
*price*. I have defined the constructors and accessor and mutator
methods in Television.java, and access it via TelevisionClient.java
(code for both is posted below). Both files compile. However when I
run TelevisionClient, it runs through the code, but my constructors,
tv0, which I assign a value in the program, and tv1, which is assigned
through dialog boxes at runtime, are the same value - in other words,
even assigning a value to one, they both have the values that are
passed in through the dialog box. I appreciate any help.
******* Television.java ****************
import java.util.*;
import javax.swing.JOptionPane;
import java.io.*;
public class Television
{
public static String brand;
public static String price;
public Television(String xbrand, String xprice)
{
brand = xbrand;
price = xprice;
}
public static String getBrand()
{
return brand;
}
public static String getPrice()
{
return price;
}
public String toString()
{
String str = "";
str = "Television information:\n"
+ "The brand name is " + this.brand + "\n"
+ "The price is: $" + price;
return str;
}
public boolean equals(Television tvx)
{
if (brand.equals(tvx.brand)
&& price==(tvx.price))
return true;
else
return false;
}
}
******* end Television.java ****************
******* TelevisionClient.java ****************
import javax.swing.JOptionPane;
public class TelevisionClient
{
public static String inputMessage;
public static String outputMessage;
public static String outputMessage2;
public static String outMsg;
Television tv0, tv1;
public void workWithTV( )
{
tv0 = new Television( "RCA", "100.00");
JOptionPane.showMessageDialog( null, tv0.toString( ) );
outputMessage = "This program will list the brand and price of a
television "
+ "based on the values you enter.\n"
+ "It will then compare the values to the default: RCA - $100.00\n";
JOptionPane.showMessageDialog(null, outputMessage,"Television
Attributes",
JOptionPane.PLAIN_MESSAGE);
inputMessage = "Enter the brand of the television:\n\n";
Television.brand = JOptionPane.showInputDialog(inputMessage);
inputMessage = "Enter the price of the television:\n\n";
Television.price = JOptionPane.showInputDialog(inputMessage);
// price = Double.parseDouble(price);
Television tv1 = new Television(Television.brand, Television.price);
outputMessage = tv1.toString();
JOptionPane.showMessageDialog(null, outputMessage,"Television
Attributes",
JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog( null, tv0.toString( ) );
JOptionPane.showMessageDialog( null, tv1.toString( ) );
outMsg = "tv0: " + tv0.brand + "/" + tv0.price + "\n"
+ "tv1: " + tv1.brand + "/" + tv1.price + "\n";
JOptionPane.showMessageDialog(null, outMsg,"Television Attributes",
JOptionPane.PLAIN_MESSAGE);
if ( tv1.equals( tv0 ) )
outputMessage2 = "DOES EQUAL";
else
outputMessage2 = "DOES NOT EQUAL";
outputMessage = "The values you entered, brand: " +
Television.getBrand() + " / price: $" + Television.getPrice() + "\n"
JOptionPane.showMessageDialog( null, outputMessage );
if ( tv1 == tv0 )
outputMessage2 = "DOES EQUAL";
else
outputMessage2 = "DOES NOT EQUAL";
outputMessage = "The values you entered, brand: " +
Television.getBrand() + " / price: $" + Television.getPrice() + "\n"
JOptionPane.showMessageDialog( null, outputMessage );
}
public static void main (String[] args)
{
TelevisionClient app = new TelevisionClient( );
app.workWithTV( );
}
}
******* end TelevisionClient.java ****************
constructors for a class, Television, whose attributes are *brand* and
*price*. I have defined the constructors and accessor and mutator
methods in Television.java, and access it via TelevisionClient.java
(code for both is posted below). Both files compile. However when I
run TelevisionClient, it runs through the code, but my constructors,
tv0, which I assign a value in the program, and tv1, which is assigned
through dialog boxes at runtime, are the same value - in other words,
even assigning a value to one, they both have the values that are
passed in through the dialog box. I appreciate any help.
******* Television.java ****************
import java.util.*;
import javax.swing.JOptionPane;
import java.io.*;
public class Television
{
public static String brand;
public static String price;
public Television(String xbrand, String xprice)
{
brand = xbrand;
price = xprice;
}
public static String getBrand()
{
return brand;
}
public static String getPrice()
{
return price;
}
public String toString()
{
String str = "";
str = "Television information:\n"
+ "The brand name is " + this.brand + "\n"
+ "The price is: $" + price;
return str;
}
public boolean equals(Television tvx)
{
if (brand.equals(tvx.brand)
&& price==(tvx.price))
return true;
else
return false;
}
}
******* end Television.java ****************
******* TelevisionClient.java ****************
import javax.swing.JOptionPane;
public class TelevisionClient
{
public static String inputMessage;
public static String outputMessage;
public static String outputMessage2;
public static String outMsg;
Television tv0, tv1;
public void workWithTV( )
{
tv0 = new Television( "RCA", "100.00");
JOptionPane.showMessageDialog( null, tv0.toString( ) );
outputMessage = "This program will list the brand and price of a
television "
+ "based on the values you enter.\n"
+ "It will then compare the values to the default: RCA - $100.00\n";
JOptionPane.showMessageDialog(null, outputMessage,"Television
Attributes",
JOptionPane.PLAIN_MESSAGE);
inputMessage = "Enter the brand of the television:\n\n";
Television.brand = JOptionPane.showInputDialog(inputMessage);
inputMessage = "Enter the price of the television:\n\n";
Television.price = JOptionPane.showInputDialog(inputMessage);
// price = Double.parseDouble(price);
Television tv1 = new Television(Television.brand, Television.price);
outputMessage = tv1.toString();
JOptionPane.showMessageDialog(null, outputMessage,"Television
Attributes",
JOptionPane.PLAIN_MESSAGE);
JOptionPane.showMessageDialog( null, tv0.toString( ) );
JOptionPane.showMessageDialog( null, tv1.toString( ) );
outMsg = "tv0: " + tv0.brand + "/" + tv0.price + "\n"
+ "tv1: " + tv1.brand + "/" + tv1.price + "\n";
JOptionPane.showMessageDialog(null, outMsg,"Television Attributes",
JOptionPane.PLAIN_MESSAGE);
if ( tv1.equals( tv0 ) )
outputMessage2 = "DOES EQUAL";
else
outputMessage2 = "DOES NOT EQUAL";
outputMessage = "The values you entered, brand: " +
Television.getBrand() + " / price: $" + Television.getPrice() + "\n"
JOptionPane.showMessageDialog( null, outputMessage );
if ( tv1 == tv0 )
outputMessage2 = "DOES EQUAL";
else
outputMessage2 = "DOES NOT EQUAL";
outputMessage = "The values you entered, brand: " +
Television.getBrand() + " / price: $" + Television.getPrice() + "\n"
JOptionPane.showMessageDialog( null, outputMessage );
}
public static void main (String[] args)
{
TelevisionClient app = new TelevisionClient( );
app.workWithTV( );
}
}
******* end TelevisionClient.java ****************