Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Please someone help me in my Assignment
Closed Thread
Old 03-20-2007, 02:22 PM   #1 (permalink)
 
Banned

Join Date: Aug 2005

Posts: 3,480

maroon1 is on a distinguished road

Default Please someone help me in my Assignment

Code:
Assignment
   -Create class Library Item
   -Create classes Book and CD that extends Library Item class
   -Create a class My Library that contains and array of 5 books and another of 5 CDs
   -Write a program to test My Library class

LibraryItem Class
   Arrtibutes:
     -Shelf (Integer)
     -Available (Boolean) 
   
   Methods:
     -Constructor
     -ReadInfo
     -PrintInfo
     -CheckAvailability


Book Class
  Arrtibutes:
    -Subject (String)
    -Author(String)
    -NumberOfPages (Integer)
   
  Methods:
     -Constructor
     -ReadInfo
     -PrintInfo
     -GetAuthor

CD Class
    Arrtibutes:
     -Artist (String)
     -NumberOfTracks(Integer)
     -Year (Integer)
   
   Methods:
      -Constructor
      -ReadInfo
      -PrintInfo
      -GetArtist

MyLibrary Class
   Arrtibutes:
    -Array of 5 books
    -Array of 5 CDs
   
   Methods:
      -Constructor
      -ReadInfo
      -PrintInfo
      -DisplayAuthorOfMaxPageBook
      -DisplayArtistOfLatestAlbum

Test Class
  -Create 1 instance of MyLibrary class
  -Read Info from user
  -Display the author of the book with maximum number of pages
  -Display the artist with the latest album
Please someone help me in my Assignment


Thanks in advance
maroon1 is offline  
Old 03-20-2007, 04:32 PM   #2 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: Please someone help me in my Assignment

So are you asking someone to do it for you? What exactly does "help" mean in this context? You do your own homework. When you get stuck, post your code and we'll help. However, no one is going to do it for you.
jaeusm is offline  
Old 03-20-2007, 05:20 PM   #3 (permalink)
 
Banned

Join Date: Aug 2005

Posts: 3,480

maroon1 is on a distinguished road

Default Re: Please someone help me in my Assignment

I'm a beginner so I need a lot of help

So far I did this code for LibraryItem, but the problem is that I don't know how to do "CheckAvailability" method. So I need help on that.

Code:
import java.util.Scanner;
public class LibraryItem

{
	
	private int Shelf;
	private Boolean Available; 
	
	public  LibraryItem()


{
	
	Shelf=0;
	Available=true;
	
}

public void ReadInfo()

{
  Scanner input = new Scanner(System.in);
  int X=1;
  System.out.println("Enter the number of the shelf:");
  Shelf=input.nextInt();	
  
  System.out.println("Click 1 if the book is available or 0 if it is not available:");
  X=input.nextInt();
  
  if (X==1)
  Available=true;
  
  if (X==0)
   Available=false;
  
  
  
}

public void PrintInfo()

{
	
	if (Available==true)

    System.out.println("The book is available");
	
	if (Available==false)
	System.out.println("The book is not available");
	
	
}

}

maroon1 is offline  
Old 03-20-2007, 10:46 PM   #4 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: Please someone help me in my Assignment

A few things I noticed:

1. Since you're a beginner, you may not be aware of programming language style guidelines, but they are important. As such, your variables should not be capitalized. The compiler doesn't care about it, but other programmers do. It may seem trivial to you at this stage, but if you stick with programming for awhile, you'll recognize the benefit. You should ask your instructor about them, or search Google for more information.

2. The 'CheckAvailability' method should return the value of the class variable 'available'. That's all. It's unfortunate that your assignment doesn't require it's use. Actually, I think this is a poor assignment to give to any student. I understand why you don't know how to implement 'CheckAvailability'. Since your task is to create a library, you should have the ability to checkout items and return items. Since you are not required to implement that functionality, it really doesn't make much sense to check the availability of an item. Perhaps you will be extending this assignment in the coming weeks.

3. In the second line of the ReadInfo method, you make an unnecessary assignment:
Code:
int X=1;
Since you are going to read in a value from the user and assign it to 'X', you don't need to assign it any value before that.

4. In the ReadInfo method, you have two consecutive and related 'if' statements. In this case, the second if statement should be an 'else if':
Code:
if (X == 1)
    available=true;
else if (X == 0)
    available=false;
5. In the PrintInfo method, there are a couple things that should be changed. First, you should use an 'else' instead of the second 'if' statement. The values of 'available' can only be true or false.

The second thing that can be changed is the expression in the 'if' statement. Since the 'if' statement will evaluate the expression in parentheses to obtain a boolean value (true or false), you don't need to compare the value of 'available' to anything because it is defined as a boolean type. So you could do this instead:
Code:
if (available)
    System.out.println("The book is available");
else
    System.out.println("The book is not available");


Last edited by jaeusm; 03-20-2007 at 10:50 PM.
jaeusm is offline  
Old 03-21-2007, 11:27 AM   #5 (permalink)
 
Banned

Join Date: Aug 2005

Posts: 3,480

maroon1 is on a distinguished road

Default Re: Please someone help me in my Assignment

Thank you for your reply

I have another problem is that I don't know how to initializes the Arrays in the MyLibrary Class

I did this, but I think it is incorrect

import java.util.Scanner;
public class MyLibrary


{

private String books[];
private String CDs[];

public MyLibrary()

{
String books[]={" "," "," "," "," "};
String CDs[]={" "," "," "," "," "};


}
maroon1 is offline  
Old 03-21-2007, 12:34 PM   #6 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: Please someone help me in my Assignment

Once you declare a variable's type, you do not need to do it again. You can explicitly initialize an array with values, which you tried to do, but in this case, you don't really want to do that. Also, these two arrays should not be of type 'String'. One array should be of type 'CD' and the other of type 'Book'. Since you know you're going to have 5 items in each array, you should be using this syntax:
private Book[] books = new Book[5];
private CD[] cds = new CD[5];
jaeusm is offline  
Old 03-21-2007, 02:21 PM   #7 (permalink)
 
Banned

Join Date: Aug 2005

Posts: 3,480

maroon1 is on a distinguished road

Default Re: Please someone help me in my Assignment

It is not working for me because I don't know with what values should I initialize the array.

import java.util.Scanner;
public class MyLibrary


{

private Book books[] = new Book[5];
private CD CDs[] = new CD[5];



public MyLibrary()

{



books[0]=??
books[1]=??;
maroon1 is offline  
Old 03-21-2007, 04:06 PM   #8 (permalink)
 
Software Developer

Join Date: Mar 2006

Location: Columbus, OH

Posts: 569

jaeusm is on a distinguished road

Default Re: Please someone help me in my Assignment

Each element in the 'Book' array should contain a 'Book' object. You created new arrays using the 'new' keyword. Now you need to create new books and cds and store them into your arrays.
jaeusm is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On