Hi all

please can someone help? i have to create an exception class (which i think i've done) and then throw it from some helper methods.

i've tried allsorts to get this exception to work, and i just cant see what i'm doing wrong....

as this is an assignment i dont want the code to be written for me, but if someone can explain a) what i'm doing wrong and b) how to do it correctly that would be great!

so:

the idea is to check 3 variables are all valid. Two are strings the other an int. if all 3 are valid the course is created. if any are not valid then an exception should be thrown with a corresponding message displayed.

code for my course class

package tma01q1;

/**
 * Title: Course class skeleton
 * Description: Complete the class to describe a university course
 * @author M257 Course Team
 * @version 1.0
 */
import java.util.*;


public class Course
{
   private String code;
   private String title;
   private int points;
   String error;

   //TODO complete the class constructor
   public Course(String aCode, String aTitle, int pts) throws InvalidCourseException
   {

      if (validCode(aCode) == true)
      {
         code = aCode;
      }
      else
      {
         error = "Incorrect Code : " + aCode;
         throw new InvalidCourseException(error);
      }


      if (validTitle(aTitle) == true)
      {
         title = aTitle;
      }
      else
      {
         error = "Missing Title : " + aTitle;
         throw new InvalidCourseException(error);
      }

      if (validPoints(pts) == true)
      {
         points = pts;
      }
      else
      {
         error = "Incorrect Course Points : " + pts;
         throw new InvalidCourseException(error);
      }

   }




   //TODO the toString method
   public String toString()
   {
      return code + ", " + title + " (" + points + ");";
   }

   //TODO the equals method
   public boolean equals(Object o)
   {
      return o.equals(o);
   }



//TODO the validCode method
   public boolean validCode(String aCode)
   {
      //check length of aCode
      if (aCode.length() != 4)
      {
         return false;
      }

      //check character types in string
      if (Character.isDigit(aCode.charAt(0)))
      {
         return false;

      }

      if(Character.isLetter(aCode.charAt(1)))
      {
         return false;

      }

      if (Character.isLetter(aCode.charAt(2)))
      {
         return false;
      }

      if (Character.isLetter(aCode.charAt(3)))
      {
         return false;
      }

      return true;
   }




//TODO the validPoints method
   public boolean validPoints(int pts)
   {
      //check that pts is a multiple of 10 between 10 and 60
      if ((pts == 10) || (pts == 20) || (pts == 30) || (pts == 40) || (pts == 50) || (pts == 60))
      {
         return true;
      }
      return false;
   }


//TODO the validTitle method
   public boolean validTitle(String aTitle)
   {
      //check title is not equal to null
      if (aTitle != null)
      {
         return true;
      }
      return false;
   }


//provided getters
   public String getCode()
   {
      return code;
   }

   public String getTitle()
   {
      return title;
   }

   public int getPoints()
   {
      return points;
   }
}

and the code for the exception

package tma01q1;
/*
 * InvalidCourseException.java
 *
 * Created on 06 January 2010, 10:37
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */


import java.util.*;
import java.io.*;
/**
 *
 * @author User
 */
class InvalidCourseException extends IOException
{

   /**
    * Creates a new instance of InvalidCourseException
    */

   public InvalidCourseException(String e)
   {

      System.out.println("Error: " + e);

   }

}

and finally the testdata that should check it works properly

package tma01q1;

/**
 * Title: TestCourse skeleton
 * Description: Use this class to test your Course class
 *
 * @author M257 Course Team
 * @version 1.0
 */

// Complete this class according to the instructions

public class TestCourse
{

   public static void main(String[] args)
   {
      final int NUM_OF_COURSES = 3;

      // create courses array
      Course[] courses = new Course[NUM_OF_COURSES];

      courses[0] = new Course("P101", "Advanced Conversation", 10); 
      courses[1] = new Course("P101", "Advanced Conversation", 20);
      courses[2] = new Course("Q202", "Computational Stuff", 30);

      //TODO Display on the screen the full details of each course,
      // for all courses in the courses array
      for (int i = 0; i < courses.length; i++)
      {
         System.out.println(courses[i].toString());
      }
      //TODO Display the text "same titles" if the the first and third courses have the same titles

      if (courses[0].getTitle().equals(courses[2].getTitle()))
      {
         System.out.println("Same Titles");
      }

      //TODO Display the text 'same courses' if the first and second courses are equal

      String course1 = courses[0].getCode() + courses[0].getTitle() + courses[0].getPoints();
      String course2 = courses[1].getCode() + courses[1].getTitle() + courses[1].getPoints();

      if (course1.equals(course2))
      {
         System.out.println("same courses");
      }

      // Test some invalid courses

//      new Course("1234", "title", 10);
//      new Course("A23", "title", 10);
//       new Course("A234","title", 2);
//       new Course("B256", null, 50);
//       new Course("M101", "title", 70);
//       new Course("a409", "course b", 0);
//       new Course("g567", "title", -10);
//       new Course("g5b7", "title", 20);
//       new Course("GC07", "title", 20);
//       new Course("G00O", "title", 20);
//       new Course(null, "title", 10);

      System.out.println("done testing the Course class");
   }
}

any help on this will be fantastic, hopefully i've just missed something simple and it'll click when someone points me in the right direction!

thanks in advance

Edited 3 Years Ago by happygeek: fixed formatting

i did a search on this page for the words 'try' and 'catch' and didnt find them.

so i do have to use a try catch block then..?

so i do have to use a try catch block then..?

your throw should be in a try.

try {

 throw;
}
catch (Exceptionclassname e) {
// can use any methods of excetion class with e object.
}

i think the throw has to throw an initialized object of the class as well. i see your doing that.
Mike

Edited 6 Years Ago by adams161: n/a

your throw should be in a try.

Not true. You can throw an Exception from anywhere.
In your test program you need to have a try/catch to catch that Exception when the called constructor throws it

How's that work exactly?

if i do:

Interger g = new Interger(5.53);

it throws an exception and if i dont put it in a try, and i look in my console i see error messages galore.

on the other hand i have heard that not all exceptions have to be caught. they are the undandled exceptions. but still yet sometimes it won't even let me compile, if i dont put certain code in a try.

Mike

There are two types of Exception - checked and unchecked (or Runtime) Exceptions. You only need a try/catch for checked Exceptions, and the compiler enforces that. Runtime Exceptions (eg NullPointerException) do not have to be declared in methods, or checked for (try/catch).

This article has been dead for over six months. Start a new discussion instead.