• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

beginner needs help

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Curse your sudden but inevitable betrayal! And this tiny ad too!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic