Assignment Classes

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

46 Replies - 1351 Views - Last Post: 17 January 2012 - 09:07 AM Rate Topic: -----

#1 mortanto  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 12-January 12

Assignment Classes

Posted 12 January 2012 - 07:16 AM

It's been quite a while since I have actually done any coding and i'm struggling to figure out what classes and methods i need for an assignment.

The assignment is to create a program that holds a collection of items (the items are different types of texts, such as books, journals etc). you need to be able to add new items, delete them and list items in the collection. It needs a text based ui as well.

I've started trying to create each of the items which can all be based on a super class so i've tried to create that

 public abstract class Item
{
    
    private String author;
    private int pubYear;
    private String titleOfPub;
    
    /**
     * requires : author != null
     * modifies : this
     * effects  : sets author, pub year,and titleOfPub  
     * @param author the item's author, year the items publication year, title the item's title
     */
    
    public Item(String author, int year, String title)
    {
        this.author = author;
        pubYear = year;
        titleOfPub = title;
       
    }
 
    
    public String getAuthor()
        {
            return author;
        }


this would be my base class but I really dont know what methods to put into it. From this i've tried to make one sub class but not sure if its right.

 public class Book extends Item
{
    // instance variables - replace the example below with your own
    private String placeOfPub;
    private String publisher;
    private int edition;
    

    /**
     * Constructor for objects of class Book
     */
    public Book(String author, int year, String title, String place, String publisher, int edition)
    {
        this.author = author;
        pubYear = year;
        titleOfPub = title;
        placeOfPub = place;
        publisher = publisher;
        edition = edition;
    }

   
}



I know asking for help like this is frowned upon but im really running out of options. I just need help in finding what goes where

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Assignment Classes

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Assignment Classes

Posted 12 January 2012 - 07:23 AM

It always helps to write out the flow of your program. A whiteboard is invaluable tool in my office. By writing it out you can see how you want your program to work and what classes will need to interact with each other. That high level approach can help you derive what methods you'll need.

For starters, finish your getters and setters. That wont take long, then you should look into a library class, and a UI class. The library will be able to index and look up all you Items and the UI class will house the main to run the program and will have the I/O to the command prompt
Was This Post Helpful? 0
  • +
  • -

#3 mortanto  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 12-January 12

Re: Assignment Classes

Posted 12 January 2012 - 07:34 AM

/**
 * Abstract class Item - Base Class for all items stored
 * 
 * @author Christopher Morton
 * @version 12/01/2012
 */
public abstract class Item
{
    
    private String author;
    private int pubYear;
    private String titleOfPub;
    
    /**
     * requires : author != null
     * modifies : this
     * effects  : sets author, pub year,and titleOfPub  
     * @param author the item's author, year the items publication year, title the item's title
     */
    
    public Item(String author, int year, String title)
        {
            this.author = author;
            pubYear = year;
            titleOfPub = title;
       
        }
 
    
    public String getAuthor()
        {
            return author;
        }
        
    public int getpubYear()
        {
            return pubYear;
        }
    public String getTitleOfPub()
        {
            return titleOfPub;
        }
        
    
}



I think that would be my abstract base class that the rest inherit from, not sure if i need to add any more to it
Was This Post Helpful? 0
  • +
  • -

#4 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Assignment Classes

Posted 12 January 2012 - 07:38 AM

Except that is not a valid abstract class.

Abstract classes cannot be instantiated. So you should be getting errors because you wrote a constructor. Now if it was a simple class that you used as a base class that is a different story.

Is there anything the Items themselves need to do? If not that is fine, since all the add/delete/etc.. will be done by your collection.
Was This Post Helpful? 0
  • +
  • -

#5 mortanto  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 12-January 12

Re: Assignment Classes

Posted 12 January 2012 - 07:41 AM

ah right thanks. Forgot about that. it is compiling tho. should i remove the constructor?

That's all they need to do, the collection would be creating the new items wouldnt it?
Was This Post Helpful? 0
  • +
  • -

#6 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Assignment Classes

Posted 12 January 2012 - 07:51 AM

Your UI should create the item and place it into your collection. Your collection will need to have methods like add / delete / search ... in order for it to do that
Was This Post Helpful? 0
  • +
  • -

#7 Shane Hudson  Icon User is offline

  • D.I.C Technophile
  • member icon

Reputation: 343
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

Re: Assignment Classes

Posted 12 January 2012 - 07:51 AM

This is a good start, however you cannot just ask us to complete it for us (though well done for providing code). If you ask particular questions, such as how to fix an error, we can help answer them.

This post has been edited by Shane Hudson: 12 January 2012 - 07:53 AM

Was This Post Helpful? 0
  • +
  • -

#8 mortanto  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 12-January 12

Re: Assignment Classes

Posted 12 January 2012 - 07:55 AM

Yeah I understand that but its been so long since I've done any coding i'm having trouble with the basics meaning i'm not entirely sure what i should be asking.

Right thanks for the help hopefully I can move on a bit.
Was This Post Helpful? 0
  • +
  • -

#9 mortanto  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 12-January 12

Re: Assignment Classes

Posted 12 January 2012 - 09:40 AM

Could any one explain how i could use a factory method pattern to create the different items? would it be possible?
Was This Post Helpful? 0
  • +
  • -

#10 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Assignment Classes

Posted 12 January 2012 - 09:45 AM

I don't think you need to follow a Factory Method Pattern, bit excessive for what your doing. However wiki has a great article on it.
http://en.wikipedia...._method_pattern
Was This Post Helpful? 0
  • +
  • -

#11 mortanto  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 12-January 12

Re: Assignment Classes

Posted 12 January 2012 - 09:49 AM

its more the requirements of the assignment, we need to show use of design patterns and i feel this would be the best one to use
Was This Post Helpful? 0
  • +
  • -

#12 mortanto  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 12-January 12

Re: Assignment Classes

Posted 12 January 2012 - 10:01 AM

if i wanted to use a factory pattern can i create the items in way that im doing now?
Was This Post Helpful? 0
  • +
  • -

#13 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: Assignment Classes

Posted 12 January 2012 - 10:02 AM

Well if your prof is going to be vauge you could just use Singleton pattern on your colleciton. That way there can only be one library to control all the items.

IMO Singleton is the easiest to implement.
Was This Post Helpful? 0
  • +
  • -

#14 mortanto  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 25
  • Joined: 12-January 12

Re: Assignment Classes

Posted 12 January 2012 - 10:04 AM

right thanks, will do that
Was This Post Helpful? 0
  • +
  • -

#15 quim  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 18
  • View blog
  • Posts: 182
  • Joined: 11-December 05

Re: Assignment Classes

Posted 12 January 2012 - 10:49 AM

you can use an adapter class. Adapter classes are also called Wrapper class. you can google this for more info.
you should also use the ArrayList class : http://docs.oracle.c.../ArrayList.html

your adapter class should look something like this:
public class List<T>
{
   // you will have an instance of array list here.

   /*you will define the necessary method (operations) of a list
   only the basics add, remove,... "and more"
   obviously you do not have to write the code for these methods 
   because array list already defined them for you.

   */
}

Was This Post Helpful? 0
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »