[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

abstract, interfaces, maps & sets, taking reading and saving to file

Hi. Basically i have a task that involves me having student. This student can have 4 units, of which each unit can have assessments. An assessment is either a test or assignment. An Assignment can be completed in group of individually. What needs to happen is that the student manager class runs the program. I have to be able to add delete & alter units, list assessments, & details of student & their units / assessments basically.  This needs to be the saved to file, & altered or viewed at any time. Assessments must be able to be viewed by due date & by unit.

i have included some of the classes i have started on. Where im stuck really is where to use TreeSet, HashMap, ArrayList (bc they r the three we have to use) & how the classes relate to each other. So really just a hand with the base classes.  Any help or pointers would be so very much appreciated.  I have the classes at the moment so that TheStudentManager controls the program. Unit Class. Contact Class (abstract) of which Student & Staff extend.  Assessment (abstract) of which Test and Assignment extend.

thanks
matt


Assessment class:

import java.util.*;
import java.io.*;

public abstract class Assessment
{
      // instance variables - replace the example below with your own
    protected String time;
    protected String date;
         
 
   
    public Assessment()
    {
        time = "unknown";
        date = "unknown";
       
    }

public Assessment(String date, String time)
    {
        this.date = date;
        this.time = time;
    }

   
   public String getDate()
   {
       return date;
   }


 
    public void setDate(String newDate)
    {
        date = newDate;
    }


   
    public String getTime()
    {
        return time;
    }
   
   
 
    public void setTime(String newTime)
    {
        time = newTime;
    }
   
 public abstract void print();
}


Test

import java.util.*;
import java.io.*;

public class Test extends Assessment
{
    private String duration;
    private String topics;
   
    public Test()
    {
        // initialise instance variables
        super("unknown", "unknown");
        duration = ("3 hours");
        topics = ("why we hate java");
    }
   
    Test(String date, String time, String newDuration, String newTopics)
    {
        super (date, time);
        duration = newDuration;
        topics = newTopics;
       
    }
   
       
    public void setDuration(String newDuration)
    {
        duration = newDuration;
    }
   
   
   
    public void setTopics(String newTopics)
    {
        topics = newTopics;
    }
   
   
       
    public void print()
    {
        System.out.println("" + "\n" +
                            "time : " + time + "\n" +
                            "duration" + duration + "\n");
    }
}
   
Assignment

import java.util.*;
import java.io.*;

public class Assignment extends Assessment
{
    private String dateDue;
    private String subject;
   
   
    public Assignment()
    {
        // initialise instance variables
        super("unknown", "unknown");
        subject = ("Java");
        dateDue = ("30th Oct");
    }

   
    Assignment(String date, String time, String newDateDue, String newSubject)
    {
        super (date, time);
        dateDue = newDateDue;
        subject = newSubject;
       
    }
   
   
   
    public void setSubject(String newSubject)
    {
        subject = newSubject;
    }
   
   
   
    public void setDateDue(String newDateDue)
    {
        dateDue = newDateDue;
    }
   
   
       
    public void print()
    {
        System.out.println("" + "\n" +
                            "time : " + time + "\n" +
                            "subject" + subject + "\n");
    }
}
   

   

Contact

import java.util.*;
import java.io.*;
public abstract class Contact
{
      // instance variables - replace the example below with your own
      protected String id;
    protected String name;
         protected String emailAddress;
    protected String address;
    protected String phoneNumber;
      
     
    public Contact()
    {
        id = "unknown";
        name = "unknown";
        emailAddress = "unknown@monash.edu.au";
        address = "unknown";
        phoneNumber = "00000000";
    }

public Contact(String newId, String newName, String newEmailAddress, String newAddress,
                           String newPhoneNumber)
    {
        // initialise instance variables
        if(!setId(newId))
            id = "unknown";
        if(!setName(newName))
            name = "unknown";
        if(!setEmailAddress(newEmailAddress));
            emailAddress = "unknown";
        if(!setAddress(newAddress))
            address = "unknown";
        if(!setPhoneNumber(newPhoneNumber))
            phoneNumber = "00000000";
    }

/**
* InvalidBlank method: checks that no fields have been left
* blank or empty
*/
   
    public boolean invalidBlank(String aString)
    {
        if (aString.trim().length()!=0)
        return true;
            return false;
    }
   
   
/**
 * This method retrieves the id of the Contact
 *
 * @return a String representing the Contact's id
 */
   
   public String getId()
   {
       return id;
   }

/**
 * This method sets the id of the contact
 *
 * @param newId the string holding the id of the contact
 * @return a boolean signifying that the id has been set
 */
   
   public boolean setId(String newId)
   {
       if(invalidBlank(newId))
       {
           id = newId;
           return true;
       }
       return false;
   }


/**
 * This method retrieves the name of the Contact
 *
 * @return a String representing the Contact's name
 */
   
   public String getName()
   {
       return name;
   }

/**
 * This method sets the name of the Contact
 *
 * @param newName the string holding the name of the Contact
 * @return a boolean signifying that the name has been set
 */
   
   public boolean setName(String newName)
   {
       if(invalidBlank(newName))
       {
           name = newName;
           return true;
       }
       return false;
   }



/**
 * This method retrieves the emailAddress of the Contact
 *
 * @return a String representing the Contact's emailAddress
 */
   
   public String getEmailAddress()
   {
       return emailAddress;
   }


/**
 * This method sets the name of the Contact
 *
 * @param newDescription the string holding the Description of the booker
 * @return a boolean signifying that the Description has been set
 */
   
   public boolean setEmailAddress(String newEmailAddress)
   {
       if(invalidBlank(newEmailAddress))
       {
           emailAddress = newEmailAddress;
           return true;
       }
       return false;
   }

/**
 * This method retrieves the street address of the Booker
 *
 * @return a String representing the Booker's street address
 */
   
   public String getAddress()
   {
       return address;
   }

/**
 * This method sets the address of the booker
 *
 * @param newAddress the string holding the address of the booker
 * @return a boolean signifying that the address has been set
 */
   
   public boolean setAddress(String newAddress)
   {
       if(invalidBlank(newAddress))
       {
           address = newAddress;
           return true;
       }
       return false;
   }



   
   public String getPhoneNumber()
   {
       return phoneNumber;
   }

 
  public boolean setPhoneNumber(String newPhoneNumber)
   {
       if(validPhoneNumber(newPhoneNumber))
       {
           phoneNumber = newPhoneNumber;
           return true;
       }
       return false;
   }

/**
 * this method validates the name so as it is not empty or balnk
 */
   
   public boolean validPhoneNumber(String newPhoneNumber)
   {
       if(newPhoneNumber.trim().length() <= 8 && newPhoneNumber.trim().length() >= 10)
       {
           System.out.println("Phone Number must contain 8-10 digits");
           return false;
       }
       return true;
       
   }
   public abstract void print();
}



Student


import java.util.*;
import java.io.*;

public class Student extends Contact
{

            public Student()
      {
            // initialise instance variables
            super("unknown", "unknown", "unknown@monash.edu.au", "unknown", "00000000" );
      }
      
      
          
    Student(String id, String name, String emailAddress, String address,
                        String phoneNumber)
    {
        super (id, name, emailAddress, address, phoneNumber);
       
    }
   


      
      public void print()
      {
          System.out.println();
      }
      

      
}



Unit


import java.io.*;
import java.util.*;

public class Unit
{
    // instance variables - replace the example below with your own
    private String unitCode;
    private String unitName;
    private Map unitAssessments;
    private String unitLecturer;
    private String unitDescription;


    public Unit()
    {
        // initialise instance variables
        unitCode = "cse0000";
        unitName = "unknown";
        unitLecturer = "unknown";
        unitDescription = "unknown";
        unitAssessments = new TreeMap();
    }
   
    public Unit(String unitCode, String unitName, String unitLecturer, String unitDescription)
    {
        this.unitCode = unitCode;
        this.unitName = unitName;
        this.unitLecturer = unitLecturer;
        this.unitDescription = unitDescription;
        unitAssessments = new TreeMap();
    }
   

   
      public void addUnitAssessments(String unitCode, int unitAssessments)
      {
          Integer nF = new Integer(unitAssessments);
          this.unitAssessments.put(unitCode,nF);
      }
      

      
      private void printUnitAssessments()
      {
          Set keys = unitAssessments.keySet();
          Iterator kI = keys.iterator();
          while(kI.hasNext())
          {
              String type = (String)kI.next();
              Integer numP = (Integer)unitAssessments.get(type);
              System.out.println(numP + " :" + type);
          }
      }      
      
 
    public void setUnitCode(String newUnitCode)
    {
        unitCode = newUnitCode;
    }


   
    public String getUnitCode()
    {
        return unitCode;
    }

   
    public void setUnitName(String newUnitName)
    {
        unitName = newUnitName;
    }
   


    public String getUnitName()
    {
        return unitName;
    }

   
    public void setUnitDescription(String newUnitDescription)
    {
        unitDescription = newUnitDescription;
    }
   

    public String getUnitDescription()
    {
        return unitDescription;
    }
 
      public void printUnit()
      {
          System.out.println("Unit Code: " + unitCode + "\n" + "Unit Name: " + unitName + "\n" +  "Number Of Assessments: ");
          printUnitAssessments();
      }
}





   

0
ca_dyl
Asked:
ca_dyl
  • 2
2 Solutions
 
WebstormCommented:
Hi ca_dyl,

Read the Java API for the classes you want (TreeSet, HashMap, ArrayList).
http://java.sun.com/j2se/1.4.2/docs/api/index.html

TreeSet is just a collection of keys. You use it to create set, and test if one value is contained in the set.

HashMap associate a key with a value. For example, you can access a student with its name :

   HashMap hm=new HashMap();

   hm.put("john",new Student("John",15,...));

   Student john=(Student)hm.get("john");


ArrayList is a kind of array whose length is variable. You use it when you know the student index in the list, or when you have to loop through all elements.

0
 
CEHJCommented:
>>
   public Assignment()
   {
       // initialise instance variables
       super("unknown", "unknown");
       subject = ("Java");
       dateDue = ("30th Oct");
   }

>>

That's not right. You wouldn't hard-code a subject and dateDue like that. Let the other ctor set these
0
 
CEHJCommented:
This will help you with the OO fundamentals:

http://java.sun.com/docs/books/tutorial/java/concepts/
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now