...

View Full Version : Help with else statments please



carefactorzer0
06-04-2008, 05:05 AM
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);
}
}

abduraooft
06-04-2008, 08:00 AM
Java and javascript are entirely different. Request a moderator to move this thread to the right sub forum.

Kor
06-04-2008, 12:39 PM
ok, moved to the Java Forum

abduraooft
06-04-2008, 01:10 PM
ok, moved to the Java ForumBut a little late! Now there are two threads :p

Kor
06-04-2008, 01:18 PM
I have no power within this Forum, I can't delete this one nor the duplicate. :rolleyes:

carefactorzer0
06-04-2008, 01:35 PM
Sorry about the double post :/

So can someone help me with trimming my constructors from four down to two ?

javabits
06-04-2008, 07:14 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum