Making Classes Work With Forms
[ BidMaker.java | CityViewHouse.java ]
wpe1.jpg (9399 bytes)

Some Instructions to get started -
New Projects - New - Visual J++ Projects - Empty Project
  Name: bidMaker  Location: *Your Disk*


Project Explorer
Right click on bidMaker - Add - Add Form - BidMaker.java
Right click on bidMaker - Add - Add Class - CityViewHouse.java

Objects are accessed indirectly using references (like pointers)

Add Member Variables to Classes --
Class Outline Window (Tab next to the Toolbox) -> Class
Right click on CityViewHouse
  Add member variable:  myHouse, datatype=CityViewHouse, access=private
  Click ADD  (you now have a pointer - myHouse - to CityViewHouse objects)

Add another member variable to the CityViewHouse class --
Right click on CityViewHouse in Class Outline Window
  Add member variable: askingPrice, datatype=int, access=private
  Click ADD

Add 2 methods to the CityViewHouse class --
Right click on CityViewHouse in Class Outline Window
  Add method:  getAskingPrice, returns an int, no parameters, public access
Right click on CityViewHouse in Class Outline Window
  Add method:  setAskingPrice, returns void, parameter=int amount, public access

Modify initialization code in bidMaker.java --
after
initForm() ...
CityViewHouse myHouse;  // myHouse is a reference to a CityViewHouse object
myHouse =
new CityViewHouse(250000);  //call to constructor

Add CityViewHouse constructor code to the CityViewHouse class definition --
Right click on CityViewHouse in Class Outline Window
  Add method: CityViewHouse, returns a void, parameter=int amount, public access
  Then delete the void from the code so the code reads
     public CityViewHouse (int amount)     //same as C++, there is no return type for a constructor


Completed Code ...

//CityViewHouse.java - Class We Define

public class CityViewHouse
{
    private int askingPrice;  //-- member variable
    public int getAskingPrice()  //-- method of the class
    {
    // TODO: replace the Builder generated code with a line of code
    // that returns the value of the member variable 'askingPrice'


    int price;
    price = askingPrice;
    return price;
    }

    public void setAskingPrice(int amount)   //--method
    {
    // TODO: replace the Builder generated code with a line of code
    // that sets the value of the member variable 'askingPrice'
    // to be the value of the parameter 'amount'

       
        askingPrice = amount;
    }

    public CityViewHouse(int amount)  //-- constructor method
    {
    askingPrice = amount;
    }
}


// BidMaker.java

import com.ms.wfc.app.*;
import com.ms.wfc.core.*;
import com.ms.wfc.ui.*;
import com.ms.wfc.html.*;

/**
* This class can take a variable number of parameters on the command
* line. Program execution begins with the main() method. The class
* constructor is not invoked unless an object of type 'BidMaker'
* created in the main() method.
*/
public class BidMaker extends Form
{
private void acceptBidBtn_click(Object source, Event e)
{
int bidPrice;
int difference;

// the following two lines of code convert the Text property
// of the priceEdt Edit control into a int value

Integer bid = new Integer(priceEdt.getText());
bidPrice = bid.intValue();

// TODO: set the 'difference' variable equal to
// the difference between the current asking price of
// myHouse and the bidPrice. Use the getAskingPrice method.

difference = myHouse.getAskingPrice() - bidPrice; //<---
       
        if (difference < 0)
{
difference = -difference;
messageBidLbl.setText("Your bid is over by ");
}
else
{
messageBidLbl.setText("Your bid is under by ");
}
bidDifferenceLbl.setText("$"+difference);
}

private CityViewHouse myHouse;
    public BidMaker()
    {
    // Required for Visual J++ Form Designer support
    initForm(); 
     
    // constructor initialization code --
    myHouse = new CityViewHouse(250000);
    askingPriceLbl.setText("$"+myHouse.getAskingPrice());
    bidDifferenceLbl.setText("$"+myHouse.getAskingPrice());
    }

    /**
    * The main entry point for the application.
    *
    * @param args Array of parameters passed to the application
    * via the command line.
    */
    public static void main(String args[])
    {
        Application.run(new BidMaker());
    }


»TOP«
Last Updated by DrB on 18 Feb 2009