Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

beginner needs help  RSS feed

 
su ma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just starting java and would appreciate help!
Is this right and how can I test it?

class Berth //class name
{

//declare the variables
private String berthId;
private int berthWidth;
private int berthLength;

//Constructor

Berth(String Bi,int Bw,int Bl)
{
setberthId(Bi);
setberthWidth(Bw);
setberthLength(Bl);

}

// ===================================


//methods

public void setberthId (String Bi)/*start the methods to set the variables
set the berth ID*/

{
berthId=Bi;
}


public void setberthWidth (int Bw)//set the berth width
{
berthWidth=Bw;
}


public void setberthLength (int Bl)//set the berth length

{
berthLength=Bl;
}

//=======================================

public String getberthId() /*start the methods to return the variables
get the berth ID*/
{
return berthId;
}


public int getberthWidth() //get the berth width//

{
return berthWidth;
}


public int getberthLength() //get the berth length//


{
return berthLength;
}

// ===================================

public void display () // method to display the information

{
System.out.println("Berth ID : " + berthId);
System.out.println("Berth Width : " + berthWidth);
System.out.println("Berth Length : " + berthLength);
}

//end methods

}//end class
 
Stefan Willi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

Yeah, the class looks fine, but remember, that you perhaps need the package declaration, if there is an package...

If you want test your code, you can simply add a static main method in your class, like the following one.


Use the following commands to compile and run your class.

javac Berth.java
java Berth



I dont know your intention by creating the display method. But, if you only want a method, which produce a String representation of your class, then override the toString() Method from the Object.class (You know, each Class in Java is extending from Object).


I like to add a further annotation!
Java loves naming conventions. EG. A getter or setter Method starts with get/is/set followed by a capitalized Letter. eg. getBerthWidth(). But this is only a recommendation.


Hope, this was usefull.

Stefan
 
su ma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou very much!



Originally posted by Stefan Willi:
Hi!

Yeah, the class looks fine, but remember, that you perhaps need the package declaration, if there is an package...

If you want test your code, you can simply add a static main method in your class, like the following one.


Use the following commands to compile and run your class.

javac Berth.java
java Berth



I dont know your intention by creating the display method. But, if you only want a method, which produce a String representation of your class, then override the toString() Method from the Object.class (You know, each Class in Java is extending from Object).


I like to add a further annotation!
Java loves naming conventions. EG. A getter or setter Method starts with get/is/set followed by a capitalized Letter. eg. getBerthWidth(). But this is only a recommendation.


Hope, this was usefull.

Stefan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!