Help! Constructors not working

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 ****************
 
L

Luch

Well, I admit I am not really clear on the static concept, but I when I
tried to compile, it told me "could not access from a non-static
reference", or something like that, on the calling line from
TelevisionClient.
 
L

Luch

Well, I admit I am not really clear on the static concept, but I when I
tried to compile, it told me "could not access from a non-static
reference", or something like that, on the calling line from
TelevisionClient.
 
S

steen

Your problem is infact the static identifier like the others have
stated.

A static variable is shared between any instance of the class, so
infact even thou you have two different instances of Television, they
infact share the price and brand. So when u set the price and brand on
tv2, you also set those in tv1. Get rid of the static identifiers and
you should be ready to go.
 
S

steen

Oh and just read your code.

when you set the price and brand, you do so on the Television class,
thats why you get the error about them needing to be static.

What you're looking for is more along the lines of :

inputMessage = "Enter the brand of the television:\n\n";
String brand = JOptionPane.showInputDialog(inputMessage);
inputMessage = "Enter the price of the television:\n\n";
String price = JOptionPane.showInputDialog(inputMessage);
Television tv1 = new Television(brand, price);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top