View Full Version : Urgent help~java
gracie 04-25-2003, 10:03 AM I'm a beginner in java ... sigh . but anyone kind enough to help me, how do change int c = ? to make this all work .
so far it can compile and execute but only the first part....
how do i get it to execute the rest as well.... ? Plz help ...
public class Land {
public Land() {
int grass = 100;
int c = 0;
if (c == 0) {
grass = grass + 3;
System.out.println (" 3 bunches of grass has grow in certain area of the pasture");
} else if ( c == 1) {
grass = grass + 0;
} else if (c == 2 ) {
grass = grass;
System.out.println ("The grass is all wet");
System.out.println ("Certain area of the pasture has puddles of water");
} else {
grass = grass -2;
System.out.println ("The grass is swaying from side to side");
System.out.println ("Leaves are falling off the trees and being blown along with the wind");
}
}
public static void main (String[] args) {
new Land();
}
}
digitok 04-25-2003, 10:12 AM Land test = new Land();
test.c = 232;
232 being the new value of 'c'.
Hope it helps.
gracie 04-25-2003, 10:16 AM digitok , what do u mean by that ...? i don't get it ..
why 232?
i jsut the int c to run through all of the conditions when possible
sorry i dun get what u mean .
plz explain thanks
digitok 04-25-2003, 10:20 AM Well I thought you wanted to change the value of 'c' ?
Can you explain exactly what you're wanting to do?
Also, I'm guessing that 'public static void main(String[] args)' is in a new file?
gracie 04-25-2003, 10:47 AM okay, i just want to change the int c = 0 at the top to satisfied all. condition..
if c=o it'll printout ( .....)
if c= 1 it'll print out (...)
but when i execute it it only and always print out the condition for c= 0 and not the rest.,
if u get me .. digitok
jb4mt 04-25-2003, 10:51 AM digitok, fyi, "public static void main ..." by no means needs to be in a new file.
gracie, to test all the conditions, you need a way to manipulate the value of c, instead of just setting it to 0, which results in "only the first part running", to loosely quote you. First you need to set the value of c, then you need to run the print statements.
What I did is change the constructor to set the value of c. And in your main method, I created a loop to go over the possible different values of c.
public class Land
{
int c;
public Land(int c)
{
int grass = 100;
this.c = c;
if (c == 0) {
grass = grass + 3;
System.out.println (" 3 bunches of grass has grow in certain area of the pasture");
} else if ( c == 1) {
grass = grass + 0;
} else if (c == 2 ) {
grass = grass;
System.out.println ("The grass is all wet");
System.out.println ("Certain area of the pasture has puddles of water");
} else {
grass = grass -2;
System.out.println ("The grass is swaying from side to side");
System.out.println ("Leaves are falling off the trees and being blown along with the wind");
}
}
public static void main (String[] args)
{
for (int i=0, n=3; i<=n; i++)
{
new Land(i);
}
}
}
rooshine 04-25-2003, 10:59 AM Gracie, it looks like you initialize c=0 in the constructor, but after that it never changes. c can never equal 1 or 2. You need to add code that changes the value of c and then checks the value again. Code in the consturctor is only ran once.
digitok 04-25-2003, 11:00 AM I thought she meant that they were seperate, ie, one works and one didn't, my mistake :)
Anyway, I've posted up there how to change the value of c,
so if that's not what you meant, then I don't exactly know what you're wanting to do.
gracie 04-25-2003, 11:15 AM jb4mt: okies i get what u mean; coz i'm doing this simulation program thing which c represent weather condition which my other ppd doing...
anyone knwo what my test class will be then ?
rooshine 04-25-2003, 11:37 AM JB's class works fine, but it creates 3 separate instances of Land with diferent weather. Is this what you want? Or do you want to create one Land class and be able to change the weather?
gracie 04-25-2003, 11:50 AM i want it to be like, if the weather condition(c) for example sunny ( c=0) then then land would be like.. system println (whatever i iwant to print out)
if the weather condition is diff then the landforms is different.. according to what i put for different weather condition,
there is only one piece of land though ..
jb4mt 04-25-2003, 11:59 AM Originally posted by rooshine
JB's class works fine, but it creates 3 separate instances of Land with diferent weather. Is this what you want? Or do you want to create one Land class and be able to change the weather?
Yeah I know. That was my tradeoff for doing this with the least amount of modification to the code, for instance I didn't have to add any methods, but rather only had to alter the constructor. This seems like some kind of homework assignment for Gracie?! So I'm not going to hand out "production quality" code.
jb4mt 04-25-2003, 12:01 PM Originally posted by gracie
i want it to be like, if the weather condition(c) for example sunny ( c=0) then then land would be like.. system println (whatever i iwant to print out)
if the weather condition is diff then the landforms is different.. according to what i put for different weather condition,
there is only one piece of land though ..
your code will be a lot more understandable, then, if you name your variable "weather" instead of "c". Likewise with the value for a weather variable: make it a string, then your if statement can be:
if (weather == "sunny")
instead of the cryptic:
if (c == 0)
gracie 04-25-2003, 12:09 PM okay i change that , but i later need to call th weather class off my member to link it . so i still do it ur way for now . tomake it easier .. right?
also,
how do i setup a testclass for it ? i read the book but dun seem to get it at all..><
gracie 04-25-2003, 12:13 PM also if i did do what u told meby changing the type
then the for loop dun work ....
jb4mt 04-25-2003, 12:13 PM do you need a seperate test class? that's typically what the main method is for. all I do below is move the main method from Land into a class called LandTest. Note, this is based on the code I posted above, I have not changed the variable names.
public class LandTest {
public static void main(String args[])
{
for (int i=0, n=3; i<=n; i++)
{
new Land(i);
}
}
}
rooshine 04-25-2003, 12:17 PM Originally posted by jb4mt
This seems like some kind of homework assignment for Gracie?! So I'm not going to hand out "production quality" code.
You know, JB, you are right! I even started to write some code to move the if/else clauses out of the constructor and into a method that changes the value of weather. But now I'm not going to do that. :D
That said, we've laid out what the next steps should be. Also, changing your variable names as JB suggested would make your code much more readable.
gracie 04-25-2003, 12:20 PM so basically ur saying to me..anything under the static void main etc is the test class right?
also was wondering if u look at my other post topic called " urgent help java"
if so . the other guy come an stop half way . sigh .. mind helping wiht that .so sorry if i took up ur time somehow .. ... wanna get this done.. asap...
jb4mt 04-25-2003, 12:21 PM Originally posted by gracie
also if i did do what u told meby changing the type
then the for loop dun work ....
Good observation! You could put the strings for the various types of weather into an array. So you could have, as an example:
String currentWeather;
String [] weatherConditions = new String[4];
weatherConditions[0] = "sunny";
weatherConditions[1] = "cloudy";
etcetera
then for the for loop you would have:
for (int i=0, n = weatherConditions.length; i<n; i++)
there's a bit more too it than this, but figuring it out should be beneficial to you.
gracie 04-25-2003, 12:28 PM hhmm.. so much to do sigh ...
so ur saying anything above, etcetera all should be delcared and
then change the conditon insde to if ( weatherConditon[0] = "sunny") etc..
right or have i misunderstand ?
dun get ur loop with then in it ..
also i was meant for the other topic post " java help" sorlie ...
jb4mt 04-25-2003, 12:55 PM Originally posted by jb4mt
Good observation! You could put the strings for the various types of weather into an array. So you could have, as an example:
String currentWeather;
String [] weatherConditions = new String[4];
weatherConditions[0] = "sunny";
weatherConditions[1] = "cloudy";
etcetera
then for the for loop you would have:
for (int i=0, n = weatherConditions.length; i<n; i++)
there's a bit more too it than this, but figuring it out should be beneficial to you.
Actually you won't want to directly manipulate the elements of the weatherConditions array once they have been set. rather, inside your loop you will want to do something like:
currentWeather = weatherConditions[i]
You don't have to do it this way, and if you're not understanding, and this is for some kind of urgent assignment, stick with what you had earlier. Then come back to these ideas later.
jb4mt 04-25-2003, 01:19 PM public class Land
{
private static final String[] weatherConditions = new String[]
{
"sunny",
"cloudy",
"rainy",
"windy"
};
private String currentWeather;
void setCurrentWeather(String weatherCondition)
{
currentWeather = weatherCondition;
}
void weatherReport()
{
if (currentWeather == "sunny") {
System.out.println("3 bunches of grass have grown in the pasture");
} else if (currentWeather == "cloudy") {
System.out.println("Gracie needs to specify cloudy weather report");
} else if (currentWeather == "rainy") {
System.out.println("The grass is all wet");
System.out.println("The pasture has puddles of water");
} else if (currentWeather == "windy") {
System.out.println("The grass is swaying from side to side");
System.out.println("Leaves are falling off trees and being blown by the wind");
} else {
System.out.println("Chili today, Hot Tamale");
}
}
public static void main (String[] args)
{
Land testLand = new Land();
for (int i=0, n = Land.weatherConditions.length; i<n; i++)
{
testLand.setCurrentWeather(Land.weatherConditions[i]);
testLand.weatherReport();
}
}
}
gracie 04-25-2003, 01:24 PM bingo yup assignment >< got it jsut two dasy ago but not due till 12 days time.. wanna get it done .coz have exams for other subject during the second week ... ><
i kind of getting it to work now for the land file... thanks guys
gracie 04-25-2003, 01:29 PM Wait guys sorlie . i got to offline now ....
need help parent.. **** ><
try back later... .
JB i post for u when i get back when i hop on kekeke sorlie
ilyash 04-25-2003, 11:37 PM gracie this is your problem:
you have c = 0;
it always stays 0.
your program never changes its value
if (c == 0) {
grass = grass + 3;
System.out.println (" 3 bunches of grass has grow in certain area of the pasture");
}
it will always print " 3 bunches of grass has grow in certain area of the pasture"
thats the problem!
jb4mt 04-26-2003, 12:28 AM Originally posted by ilyash
gracie this is your problem:
you have c = 0;
it always stays 0.
your program never changes its value
if (c == 0) {
grass = grass + 3;
System.out.println (" 3 bunches of grass has grow in certain area of the pasture");
}
it will always print " 3 bunches of grass has grow in certain area of the pasture"
thats the problem!
ilyash, did you bother to read ANY of the other responses? we are WAY beyond that already.
gracie 04-26-2003, 09:23 AM okies
not talk_to_tree is also me ..i got two account ,,
public class Sheep1 {
private int Energy;
public int i;
public void walkSheep1() {
System.out.println("Sheep walking around pasture");
for ( i = 0; i <= 10; i= i++) {
if (Energy <=5) {
changeEnergy();
} else {
walkSheep1();
}
}
}
public void changeEnergy() {
for (i=0; i <= 10; i = i++) {
Energy = Energy + i;
if ( Energy <= 5) {
EatSheep1();
} else if (Energy >= 10) {
Energy = 10;
walkSheep1();
} else {
DrinkSheep1();
}
}
}
public void EatSheep1() {
Energy = Energy + 1;
System.out.println ("Sheeps are eating grass");
changeEnergy();
}
public void DrinkSheep1() {
Energy = Energy + 1;
System.out.println ("Sheeps are drinking water");
changeEnergy();
}
public static void main ( String[] args) {
new Sheep1();
}
}
1. it won't read executeanything
ilyash 04-26-2003, 11:05 AM all you did was create a new instance of Sheep1
it wasnt supposed to execute anything
it will just do:
private int Energy;
public int i;
they are intiated but no default value is set...
gracie 04-26-2003, 07:04 PM then what does my priinting statement do, in the method then ... ? when will they display ?
ilyash 04-26-2003, 10:47 PM in your sheep1 class have a constructor..
make that constructor have the if's and else's
for example...
if (Energy == null)
//get food/drink
else if (Energy > 6)
//do exercise
etc
gracie 04-26-2003, 11:35 PM sorry i dun get it .. explain plz.. also dun iahve one already ..
|