Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble communicating between user-defined classes 3

Status
Not open for further replies.

MissouriTiger

Programmer
Oct 10, 2000
185
US
I have created 3 classes, as follows:

1. An applet which will call methods of the other two.

2. StringToInt, which converts a string to int datatype.

3. CalcBestPlan, which performs decision/calculation, giving results in multiple variables.

My problems are as follows:

1. I am confused as to how to call the main methods of classes 2 and 3 (above) from class 1. You will probably be able to see my confusion by looking at my code. I am using constructors, even though I don't think that's the right way. But I cannot find any other way to do it.

2. StringToInt has a problem in the catch part. It's suposed to give an alert window if it encounters non-numeric input. I cannot seem to get it exactly right.

Could someone please show me how to call these other classes to use their Main methods, and troubleshoot. I'm posting a lot of code here. Is this apropriate? If not please let me know.

Here's my code:

*************************************
Applet's actionPerformed method which calls the other classes.
*************************************
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btnCalc)
{
StringToInt Converter;

strDayMin = txtDayMin.getText();
intDayMin = (Converter = new StringToInt(strDayMin));
strEveMin = txtEveMin.getText();
intEveMin = StringToInt(strEveMin);

CalcBestPlan Calculator = new CalcBestPlan(intDayMin, intEveMin);

displayResults();

}
}


*********************************************************
CalcBestPlan Class. As you can see, there are many variables which I need to access from the applet. Is there anything wrong with the way I have declared this one?
*********************************************************
public class CalcBestPlan
{
String strPlan1Name = "Free Eve/Weekend";
String strPlan2Name = "Normal";
String strPlan3Name = "High volume";
String strBestPlanName = "NoName";

double dblPlan1Fee = 10.00;
double dblPlan2Fee = 0.00;
double dblPlan3Fee = 5.00;
double dblBestPlanFee;

double dblPlan1DayRate = 35;
double dblPlan2DayRate = 25;
double dblPlan3DayRate = 15;
double dblBestPlanDayRate;

double dblPlan1EveRate = 0;
double dblPlan2EveRate = 10;
double dblPlan3EveRate = 5;
double dblBestPlanEveRate;

public void main (int intDayMin, int intEveMin)
{
if (intDayMin > intEveMin)
{
if (dblPlan1DayRate < dblPlan2DayRate)
{
if (dblPlan1DayRate < dblPlan3DayRate)
strBestPlanName = strPlan1Name;
else
strBestPlanName = strPlan3Name;
}
else
{
if (dblPlan2DayRate < dblPlan3DayRate)
strBestPlanName = strPlan2Name;
else
strBestPlanName = strPlan3Name;
}
}

else if (intEveMin == intDayMin)
{
if (dblPlan1EveRate < dblPlan2EveRate)
{
if (dblPlan1EveRate < dblPlan3EveRate)
strBestPlanName = strPlan1Name;
else
strBestPlanName = strPlan3Name;
}
else
{
if (dblPlan2EveRate < dblPlan3EveRate)
strBestPlanName = strPlan2Name;
else
strBestPlanName = strPlan3Name;
}

}

else if (intEveMin > intDayMin)
{
if (dblPlan1EveRate < dblPlan2EveRate)
{
if (dblPlan1EveRate < dblPlan3EveRate)
strBestPlanName = strPlan1Name;
else
strBestPlanName = strPlan3Name;
}
else
{
if (dblPlan2EveRate < dblPlan3EveRate)
strBestPlanName = strPlan2Name;
else
strBestPlanName = strPlan3Name;
}
}

if (strBestPlanName == strPlan1Name)
{
dblBestPlanFee = dblPlan1Fee;
dblBestPlanDayRate = dblPlan1DayRate;
dblBestPlanEveRate = dblPlan1EveRate;
}

else if (strBestPlanName == strPlan2Name)
{
dblBestPlanFee = dblPlan2Fee;
dblBestPlanDayRate = dblPlan2DayRate;
dblBestPlanEveRate = dblPlan2EveRate;
}

else if (strBestPlanName == strPlan3Name)
{
dblBestPlanFee = dblPlan3Fee;
dblBestPlanDayRate = dblPlan3DayRate;
dblBestPlanEveRate = dblPlan3EveRate;
}

//strBestPlanName = &quot;test&quot;;
//return strBestPlanName;
}
}

******************************************************
StringToInt Class. Returns an int. It won't compile.
******************************************************
public class StringToInt
{
public int main (String strString)
{
try
{
int intString = Integer.parseInt(strString.trim());
return intString;
}

catch (NumberFormatException nfe)
{
return 000;
//mainWindow.eval(&quot;alert('The requested operation can only be performed on a numeric value. Please be sure that all inputs are valid.')&quot;);
//System.out.println(&quot;NumberFormatException: &quot; + nfe.getMessage());
}
}
}

I would be grateful for any help anyone can offer,

Greg





 
Hi Greg,

Well guess what, its me again :)

Ok, I noticed some problems you are encountering. First, you concept is not correct, pardon me if I am wrong.

What I mean by wrong concept is that there is no need to create multiple classes in order to get things done. There are lots of ways to do so (like creating methods instead).

I think I would roughly give you an example of how OOP actually works. For example, I want to create an application for car dealers. So first thing, what are the classes we need? We should think in terms of real objects so the classes we will have is:-

Car - represents Car object
Engine - represents the car engine
wheels - represents the car engine
etc.

So next thing is what variables and methods would be necessary for the particular classes? For the class Car, of course we will need the car number,car model, car brand, the price of the car etc. Since each Car has 1 engine, we will need an Engine Object as well. So it would be something like:-

public class Car
{
String carNo;
String model;
String brand;
int price;
Engine eng;
...
public Car(String carNo, String model, String brand, int price, Engine eng)
{
}
}

The same will go for other classes. Now we come to methods. Most of the time, we will require the model of a Car Object so normally you will have Get and Set methods.
Get methods are used to get the variable you want to return and Set methods will set the value of the particular variable. We can also have methods like calculating the price of that particular Car. So they would be something like:-

public String getModel()
{
return model;
}
public void setModel(String model)
{
this.model = model;
}
public int calculateCost()
{
...
}

So after creating the necessary classes with the appropriate methods, we will then proceed on the Car Dealer class, which we will call CarDealer. Since a car dealer can deal with many different cars, we will have a list of cars that they buy or sell. They will definately have their own company name etc. Since the list of cars are not always the same, we will have the methods addCar and removeCar. So the class will look like this:-

public class CarDealer
{
String name;
Vector carList;
public CarDealer(String name)
{
this.name = name;
carList = new Vector();
}
public void addCar(Car c)
{
}
public void removeCar(Car c)
{
}
}

After completing the CarDealer class, we will then come to the main class which is CarDealerSystem. This CarDealerSystem class will deal with input and output to and from users. So this CarDealerSystem will therefore have a CarDealer object. Then through the user's interaction, he/she will then add/remove new cars to this program etc.

So by now you should roughly get the idea of when to create class and methods. For your case, there is no need to create the 2 classes. It would be better to have 2 methods instead.

Your program should be compiling fine but it wouldn't be too appropriate to use.

So your program will now be like:-

// import necessary stuffs here
...
public class newApplet extends Applet
{
public void init()
{
...
}
public int StringToInt(String temp)
{
try
{
return (new Integer(temp)).intValue();
}
catch (NumberFormatException e)
{
}
}

// depending on what you want the method to do, you need or need not return/pass in parameters
public void CalBestPlan()
{
...
}
}

Since you said you need a lot of variables for calculating the best plan, you can of course declare variables within the CalBestPlan() method. If you need to use variables from the class applets, you can also use them freely since you are using methods now instead of classes.

To clarify the difference between classes and methods if you still have, classes normally have constructors and methods (a class without constructors or methods will still be able to compile and execute) . Methods have nothing but variables and process data. If your method is not implemented/empty, you will have to declare them as abstract (a higher level which I guess I wouldn't cover yet).

In order to access variables or methods from classes, you need to have an instance of the class (if you didn't declare the method or variable as static and your class as private).

So lets say if you want to get the model of a Car object, you will need to do this:-

String model = c.getModel();

c - a car object

There are lots of differences between methods and classes. There are also a lot of ways to declare them (for example, private, abstract etc). I suggest it would be good to get some books and read up on them :)

And also, I find that it is good to post your program codes onto the forum so it will be easier to see which part of your program goes wrong. I guess there is no harm doing so too.

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
I forgot to mention about constructors :)

public class Car
{
public Car()
{
}
}

public Car() is a constructor. Within a class you can have a lot of constructors like:-

public class Car
{
public Car()
{
}
public Car(String model)
{
}
public Car(String model, String brand)
{
}
...
}

Like I mentioned in the previous post, it is ok even if you didn't declare any constructor

public class Car
{
// methods
...
}

The compiler will take &quot;create&quot; a default constructor for you which is ' public Car() { }'

Hope the posts won't put you to sleep :) If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Hi Leon,

Once again, thanks. However, this is a school project and my instructor insists that we must define at least 2 classes. I agree with you that most of this can be done in one class, so I will put the CalcBestPlan back into the main Class as a method.

But the StringToInt will have to be a seperate class, in order to satisfy my teacher. Also, I feel that it's best to create a StringToInt object sonce this is a very common function - it is a very handy thing to have around. I believe this is a good, sound example of encapsulation & specialization.

Could you please fix what is wrong with my StringToInt Class. It won't compile. Would you also examine my code below which attempts to instatiate the StringToInt Class. I can't get it to work.

I appreciate all of your help. Now I must begin reading about arrays. I'm supposed to turn it in tomorrow with arrays in it. Obviously I will be turning it in late (again).

Thanks again,

Greg

 
You mustn't overwrite the main class with a return type other than void - that won't work. Overloading only works for the parameters of a function.

The class would look as follows:

Code:
public class StringToInt
{
   public static int convertToInt(String strString)
   {
      try
      {
         int intString = Integer.parseInt(strString.trim());
              return intString;
      }
      
      catch (NumberFormatException nfe)
      {
          return 000;
         //mainWindow.eval(&quot;alert('The requested operation can only be performed on a numeric value. Please be sure that all inputs are valid.')&quot;);
         //System.out.println(&quot;NumberFormatException: &quot; + nfe.getMessage());
      }
   }
}

You would then call that function as follows:

Code:
int myInt = StringToInt.convertToInt(myString)

This is possible because of the static modifier - it creates a class method instead of an object method. That means, you can't access any instance variables in the class. Does not hurt you here, as there doesn't need to be an object that instantiates the StringToInt class and therefore you won't ever even have instance variables.
The good thing about static methods is that you can call them as in the example above - just by writing the class name and then after the point the method.

Look at the following:

Code:
System.out.println

Here the System class has a static inner class variable that is called &quot;out&quot; (don't confuse this with the class being an inner class, that doesn't necessarily have to be), and this static inner class variable has a static method &quot;println&quot;. That's what static methods are good for - and you just can use one yourself :)

But: take care. Good OOP does seldom need such static approaches - these are rather a compromise to procedural programming... allow thyself to be the spark that lights the fire
 
I did what you said, but it's not working. The main class won't compile. I'm getting the following error:

SaveLongMoney.java:195: cannot resolve symbol
symbol : variable StringToInt
location: class SaveLongMoney
intDayMin = StringToInt.convertToInt(strDayMin);

Why does it think StringToInt is a variable, rather than a class?

Also, in the catch portion of the StringToInt Class I want it to open an alert window. I found the following code on the Web. It's supposed to be making a javascript call or something, but it doesn't work. Can you tell me what's wrong with it? Or is there another way to do it?

catch (NumberFormatException nfe)
{
mainWindow.eval(&quot;alert('The requested operation can only be performed on a numeric value. Please be sure that all inputs are valid.')&quot;);

}



 
Have you imported the package with the StringToInt class in it?

If you have a JRE 1.2 or above you can use the following:

JOptionPane.showMessageDialog(null, &quot;message&quot;, &quot;title&quot;, JOptionPane.ERROR_MESSAGE);

You'll have to import javax.swing.*; for this...

Remember: import statements are right below the package statement (which in turn is the first one in any class).

Your code fragment does write a javascript command into a variable called mainWindow - I don't know JSPs all too well, and I think this only works in those... allow thyself to be the spark that lights the fire
 
The import will not work. It won't compile. Somebody please tell me how to imort this darn thing.

StringToInt is by default in the Object package.

&quot;import java.lang.Object.StringToInt&quot; will not compile.

Why not? What do I do?

This is driving me absolutely crazy!!! I'm sick to death of this game! I could have written a program 10 times as complicated as this in half the time in VB. Can I please just email somebody my code and you try and run it and figure out what the h*** is wrong with it?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top