Learning Help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Learning Help

Hybrid View

  1. #1
    Join Date
    Mar 2005
    Posts
    2

    Learning Help

    Hi Everybody,

    Im doing a course that involves learning java, and im findind it abit tough. I have a piece of work to do, which involves writing a class that tests another class to see if its methods work, and i have no idea where to start. I heard from a pal this is a great place to get advice, so thought id see if anybody could help. Thanks in Advance.

    Carl
    Last edited by Carlos; 03-08-2005 at 10:21 AM.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Ok, I'll try to keep it simple

    This is the Car class and a class to test its methods.

    Code:
    /**
     * Class for testing the Car class.
     * This class doesn't need a constructor, but I've supplied
     * a default (empty) constructor here
     */
    class CarClassTester {
      public CarClassTester() {} // empty constructor
    
      public void runTest() {
        // make some cars
        Car aCar=new Car("Ford","Focus",2003);
        Car anotherCar=new Car("Volvo","V70",2004);
        // get the make, model and year of one of the cars
        // and print it out.
        String mk=aCar.getMake();
        String md=aCar.getModel();
        int yr=aCar.getYear();
        System.out.println("Car data, Make: "+mk+", Model: "+md+", Year: "+yr);
        // use the toString method of both car objects.  
        System.out.println(anotherCar.toString());
        System.out.println(aCar.toString());
      }
      // the main method, this is where the program execution starts.
      // the definition of this method requires the String array parameter
      // but that array is not used here.
      public static void main(String[] args) {
        // create a CarClassTester object
        CarClassTester cct = new CarClassTester();
        // invoke the CarClassTester objetcs only method.
        cct.runTest();
        // and we are finished ....
      }
    }
    
    /**
     * Class for storing basic car information
     */
    class Car {
      private String make = null;
      private String model = null;
      private int year = -1;
    
      // constructor
      public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
      }
    
      // the toString method, delivers a string explainig in
      // short what the object contains
      public String toString() {
        return "Make: " + make + ", Model: " + model + ", Year: " + year;
      }
    
      // get methods
      public String getMake() {
        return make;
      }
    
      public String getModel() {
        return model;
      }
    
      public int getYear() {
        return year;
      }
    }
    eschew obfuscation

  3. #3
    Join Date
    Mar 2005
    Posts
    2
    Thanks that really helped me to understand what i have to do, however im getting a few porblems you could perhaps help iron out, heres my Class, and my Class tester;

    Code:
    /**
     * Details of Technical support calls.
     * The details of the calls are a reference number, the call's priority,
     * the email address of the caller and a description of the call.
     * 
     */
    
    public class Call
    {
    
      private int number;
      private int priority;
      private String caller;
      private String description;
    
      /**
      * Class Constructor. 
      * Setting default values.
      */
      public Call ()
      {
        number = 0;
        priority = 0;
        caller = "";
        description = "";
      }
    
      /**
       * Class Constructor. 
       * Set initial values to those provided as parameters.
       *
       * @param number Initial number.
       * @param priority Initial priority.
       * @param caller Initial caller name.
       * @param description Initial call description.
       */
      public Call (int number, int priority, String caller, String description)
      {
        this.number = number;
        this.priority = priority;
        this.caller = caller;
        this.description = description;
      }
    
      /**
       * Accessor for Call Reference Number.
       *
       * @return Call reference number.
       */
      public int getNumber ()
      {
        return number;
      }
    
      /**
       * Accessor for Call Priority.
       *
       * @return Call priority.
       */
      public int getPriority ()
      {
        return priority;
      }
    
      /**
       * Accessor for Call Caller.
       *
       * @return Call's caller (email address).
       */
      public String getCaller ()
      {
        return caller;
      }
    
      /**
       * Accessor for Call Description.
       *
       * @return Call description.
       */
      public String getDescription ()
      {
        return description;
      }
    
      /**
       * Mutator for Call Reference Number.
       *
       * @param number New call reference number.
       * @return true if call reference number is set, false otherwise.
       */
      public boolean setNumber (int number)
      {
        if (number > 0) {
          this.number = number;
          return true;
        }
    
        return false;
      }
    
      /**
       * Mutator for Call Priority.
       * Call priority must be 1, 2, 3, or 4. The mutator enforces this, and
       * returns a value to show whether an invalid value is provided.
       *
       * @param priority New call priority.
       * @return true if call priority is set, false otherwise.
       */
      public boolean setPriority (int priority)
      {
        if (priority >= 1 && priority <= 4) {
          this.priority = priority;
          return true;
        }
        else {
          return false;
        }
      }
    
      /**
       * Mutator for Call Caller.
       * The caller must (appear to) be a valid email address. The mutator
       * attempts to enforce this by checking that it contains an @ symbol
       * and at least one dot.
       *
       * @param caller New call caller.
       * @return true if caller is set, false otherwise.
       */
      public boolean setCaller (String caller)
      {
        if (caller.indexOf ('.') == -1 || caller.indexOf ('@') == -1) {
          return false;
        }
        else {
          this.caller = caller;
          return true;
        }
      }
    
      /**
       * Mutator for Call Description.
       * The description is any non-zero sequence of characters.
       *
       * @param description New call description.
       * @return true if description is set, false otherwise.
       */
      public boolean setDescription (String description)
      {
        if (description.length () > 0) {
          this.description = description;
          return true;
        }
        else {
          return false;
        }
      }
    
      /**
       * String Representation.
       * The call details are formatted in a small table:<br>
       * <pre>
       * Call#: 0 (Priority: 0) (Caller: john@doe.com)
       * Description:
       *   Example call.
       * </pre>
       * @return String representation.
       */
      public String toString ()
      {
        String c = "";
    
        c += "Call# " + number + " (Priority: " + priority + ") ";
        c += "(Caller: " + caller + ")\n";
    
        c+= "Description:\n" + description + '\n';
    
        return c;
      }
    }
    Thats the class i need to write a tester for, and heres what i came up with thanks to your help;

    Code:
    /**
     * Class for testing the Call Methods
     */
    class CallTest {
      public CallTest() {} // constructor
    
      public void runTest() {
        // Add some calls
        Call aCall=new Call("1","4","test@test.com","Broken Monitor");
        Call anotherCall=new Call("2","1","test2@test2.com","Computer on Fire");
        // get the number, priority, email address and Description of the call
        // and print it out.
        int num=aCall.getNumber();
        int pri=aCall.getPriority();
        String caller=aCall.getCaller();
        String descrip=aCall.getDescription();
        System.out.println("Call Data, Reference Number: "+num+", Priority: "+pri+", Caller Id: "+caller+", Description: "+descrip);
        // use the toString method of both call objects.  
        System.out.println(anotherCall.toString());
        System.out.println(aCall.toString());
      }
      // the main method, runs the program and tests it.
      public static void main(String[] args) {
        // create a CallTest object
        CallTest cct = new CallTest();
        // invoke the CallTest objetcs only method.
        cct.runTest();
        // tests are run
      }
    }
    However, when i try to compile it, i get the error "cannot find symbol constructor.Call" on lines 9 and 10. Any ideas? Thanks again for such excellent help

    Carl

    EDIt : Realised i was quoting the intergers! Thanks for the help!!!
    Last edited by Carlos; 03-08-2005 at 02:18 PM.

  4. #4
    Join Date
    Jul 2005
    Location
    the Netherlands
    Posts
    128
    Dear Carl,

    You are trying to contsruct a Call-object with four String's:

    Code:
    Call aCall=new Call("1","4","test@test.com","Broken Monitor");
    Call anotherCall=new Call("2","1","test2@test2.com","Computer on Fire");
    However, the right way to create a Call-object is two int's followed by two String's. An int is a natural number and a String is a list of characters.

    This is how to tell java:
    Code:
    int i = 1;
    double d = 1.0;
    char c = 'a';
    String s = "testing 1 2 ...";
    So, the correct way to construct a Call-objetc is like this:

    Code:
    Call aCall=new Call(1,4,"test@test.com","Broken Monitor");
    Call anotherCall=new Call(2,1,"test2@test2.com","Computer on Fire");
    Good luck with your class!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center