Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-04-2008, 05:05 AM   PM User | #1
carefactorzer0
New to the CF scene

 
Join Date: Jun 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
carefactorzer0 is an unknown quantity at this point
Help with else statments please

Hey Guys, (first post!)

I have an assignment on a Library class i think i'm almost there except for the fact I have four constructors for four methorods and only two are required.

I have been fiddling around with if else for the last week with little luck below are my objectives and the program (libraryitem) and tester (libraryitemtest).

I am still getting confused between constructors and methods i have read a few online books and asked my teacher also for help but i still don't get it.

The code below works, I tried to use if else but made a mess of it and it was a little too confusing so i have left it out.

Below is are the requirements and code:

a) Attributes title, author, media, status,cost . Choose Appropriate data types.

b) Code a constructor to handle 5 inputs.

c) Code a constructor to handle title and author only, Otherwise default values.

d) Code methods to set media. status and cost.

e) Code a method to display author and title of all overdue items.

f) Code a method to display all data of an object.
-----------------------------------------------------------------
public class libraryitem {

String title;
String author;
String media;
String status;
Double cost;


libraryitem (String tit, String aut, String med, String sta, Double cos) {

title=tit;
author=aut;
media=med;
status=sta;
cost=cos;
}


libraryitem (String tit, String aut) {

title=tit;
author=aut;
}


libraryitem (String med, String sta, Double cos) {


media=med;
status=sta;
cost=cos;

}

libraryitem (String med, String sta, String tit) {

media=med;
status=sta;
title=tit;
}

public String geteverycat() {
return title+ ", " + author + ", "+media+ ", " +status+ ", "+cost;

}

public String gettitleauthonly() {
return title+ ", " +author;


}


public String getmedstacosonly() {
return media+ ", " +status+ ", "+cost;


}

public String getmedstatitonly() {
return media+ ", " +status+ ", "+title;

}


}

-----------------------------------------------------------------
public class libraryitemtest {
public static void main ( String [] args){

libraryitem libraryitemtestone = new libraryitem ("Java Programming","Vikki","Book","Onloan",35.95);
libraryitem libraryitemtesttwo = new libraryitem ("Visual Basic Programming","Bob Down");
libraryitem libraryitemtestthree = new libraryitem ("DVD","Overdue",23.45);
libraryitem libraryitemtestfour = new libraryitem ("Magazine","OnShelf","C++ Tips");

String allcat;
String titaut;
String medstacos;
String medstatit;

allcat=libraryitemtestone.geteverycat();
titaut=libraryitemtesttwo.gettitleauthonly();
medstacos=libraryitemtestthree.getmedstacosonly();
medstatit=libraryitemtestfour.getmedstatitonly();


System.out.println("TITLE AUTHOR MEDIA STATUS AND COST = "+allcat);
System.out.println("TITLE AUTHOR = "+titaut);
System.out.println("MEDIA STATUS AND COST = "+medstacos);
System.out.println("MEDIA STATUS TITLE = "+medstatit);
}
}
carefactorzer0 is offline   Reply With Quote
Old 06-04-2008, 08:00 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Java and javascript are entirely different. Request a moderator to move this thread to the right sub forum.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 06-04-2008, 12:39 PM   PM User | #3
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
ok, moved to the Java Forum
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 06-04-2008, 01:10 PM   PM User | #4
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
Originally Posted by Kor View Post
ok, moved to the Java Forum
But a little late! Now there are two threads
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 06-04-2008, 01:18 PM   PM User | #5
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
I have no power within this Forum, I can't delete this one nor the duplicate.
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 06-04-2008, 01:35 PM   PM User | #6
carefactorzer0
New to the CF scene

 
Join Date: Jun 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
carefactorzer0 is an unknown quantity at this point
Sorry about the double post :/

So can someone help me with trimming my constructors from four down to two ?
carefactorzer0 is offline   Reply With Quote
Old 06-04-2008, 07:14 PM   PM User | #7
javabits
New Coder

 
Join Date: May 2007
Location: SF, CA
Posts: 55
Thanks: 0
Thanked 4 Times in 4 Posts
javabits is on a distinguished road
You need to identify the constructors. Constructors in java are pretty easy to identify as they have the same name as the class. So your class is named libraryitem, so you should start there. You should find five different classes.

After you've identified the constructors you can look at the requirements for b and c.

b) Code a constructor to handle 5 inputs.

c) Code a constructor to handle title and author only, Otherwise default values.

So of the two constructors that you want to keep, you want one that has five parameters (the parameters are in the parentheses) and the other has two (a title and author).

Hope this helps.

semper fi...

javabits
javabits is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 04:21 AM.


Advertisement
Log in to turn off these ads.