Sign in to follow this  
Followers 0
Vexorz

Java Program

8 posts in this topic

class plants {

private String name;

private String scientific;

private String typeofplant;

private int height;

private String lighting;

private int price;

public void ChangeInValues(String new_name, String new_scientific, String new_typeofplant,int new_minheight, int new_maxheight,String new_lighting,int new_price )

{

name = new_name;

scientific = new_scientific;

typeofplant = new_typeofplant;

height = new_height;

lighting = new_lighting;

price = new_price;

}

public void outputs(){

System.out.println("name: " + name);

System.out.println("scientific: " + scientific);

System.out.println("typeofplant: " + typeofplant);

System.out.println("height:" + height);

System.out.println("lighting:" + lighting);

System.out.println("price"+ price);

}

public static void main(String args[]) {

plants peach = new plants();

peach.ChangeInValues("peach","prunus persica", "NA",5,10,"direct sun",20 );

peach.outputs();

}

}

Hey all, there is my code; however, I am wondering how I could improve my code. The project was to make a program that displays those 6 variables and have a set of accesors/mutators along with 2 constructors. I am very new to programming and Java is my first language so I am pretty clueless about everything. One question I have is how would I get that to display the height as 5-10 meters and also have the price in dollars. Any help would be greatly appreciated!

0

Share this post


Link to post
Share on other sites

You might get a better response in the Programming/Code forum. Then again, they may just think you're trying to get help with your homework ;)

0

Share this post


Link to post
Share on other sites

There's a ton of things wrong with your code. Let's start with your 2 constuctors (that you don't have).

Since your class is called "plant", those should be

public plant() {...}

and

public plant(String new_name, String new_scientific, String new_typeofplant,int new_minheight, int new_maxheight,String new_lighting,int new_price) {...}

Second, a set of accessors and mutators for each variable means each variable gets it's own set and get method.

Example:

void setName(String name) {...}
String getName() {...}

Once you get that stuff hammered out it should be easy to display the height and price..

Edited by chefninja
0

Share this post


Link to post
Share on other sites

hmm as the guy that posted before me said but since it's homework I won't help you

0

Share this post


Link to post
Share on other sites

There's a lot that can be approved in the structure... You should have a separate main class file and define your plant in its own class. What you have is OK if you only ever want to print the info to shell but what if one day you wanted to add a gui... You should have set and get variables for all of your variables. When defining the class you need to be thinking about what the overall project is going to call for...

Remember an object is an instance of a class... Your going to need to add functions inside your class to make your object useful... Looking over it real quick alls you have is a print function.

If this isn't homework your likely going to want to build multiple constructors that take different input... Get functions so you can call the private variables of the object and set functions so you can redefine the objects variable. If it is homework I hope your not going to have to use this code as a base to learn inheritance.....

If your not following me... By having a proper constructor you could call your object and set all of the variables at the same time.... You can also have multiple constructors for an object so you can set some values to default... Try

public void plants(String new_name, String new_scientific, String new_typeofplant,int new_minheight, int new_maxheight,String new_lighting,int new_price )
{
name = new_name;
scientific = new_scientific;
typeofplant = new_typeofplant;
height = new_height;
lighting = new_lighting;
price = new_price;
}
public void output(){
System.out.println("name: " + name);
System.out.println("scientific: " + scientific);
System.out.println("typeofplant: " + typeofplant);
System.out.println("height:" + height);
System.out.println("lighting:" + lighting);
System.out.println("price"+ price);
}
public void setName(String new_name){
this.name = new_name;
}
public String getName(){
return this.name;
)

and so on for the rest of your variables... Its not required but its good programing practice to do this and it will make your classes more portable as you develop... So now when you need to reuse this class the objects are more functional.... then in your main class

public static void main(String args[]) {
plants peach = new plants("peach","prunus persica", "NA",5,10,"direct sun",20 );
peach.output();
// But now your more flexible and can do
peach.setName("Peach");
// Or just print one part of your plant
System.out.println(peach.getName());
}

Mind you I haven't tried compiling this... its almost 2 am and hell its probaly your homework but you should get the idea with this... Hope it helps though

0

Share this post


Link to post
Share on other sites

Here is my new code, but I am uncertain as to why it is not displaying the values I set in the main function. If anyone could point out where this is happening I would be grateful.

public class plantproject{

private String typeofplant,lighting,scientific,name;

private int height;

private double price;

public plantproject(int h,double p, String t, String l, String sn, String n)

{height=h;price=p;typeofplant=t;lighting=l;scientific=sn;name=n;}

public plantproject()

{height=0;price=0.0;typeofplant ="";lighting="";scientific="p";name="";}

public String gettypeofplant() {return typeofplant;}

public String getlighting () {return lighting;}

public String getscientific () {return scientific;}

public String getname () {return name;}

public int getheight () {return height;}

public double getprice () {return price;}

public static void main(String arguments[]){

plantproject peach;

peach = new plantproject(120,20.50,"na","direct sun","prunus persica","peach");

System.out.println("height:"+peach.getheight());

System.out.println("price:"+peach.getprice());

System.out.println("typeofplant:"+peach.gettypeofplant());

System.out.println("lighting:"+peach.getlighting());

System.out.println("scientific:"+peach.getscientific());

System.out.println("name:"+peach.getname());

}

}

Edited by EECS
0

Share this post


Link to post
Share on other sites

it works fine for me.

output:

height:120

price:20.5

typeofplant:na

lighting:direct sun

scientific:prunus persica

name:peach

Press any key to continue . . .

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0