Tuesday, July 28, 2009

Need Help from someone who's familiar with programming...PLEASE!!!!?

Typed below are two programs i've typed.





1st program:





class Sum{


int x =10, y = 20;


int total = x+y;


public static void main(String args []){


System.out.println(total);


}


}





2nd Program





class Test{





public static void main(String args[]){





char name = 'W';


int bro = 1, sis = 0, total = 0, ftotal = 0;


total = sis+bro;


ftotal = total+3;





System.out.print ("My name is: ");


System.out.println(name);





System.out.print ("I have ");


System.out.print (bro);


System.out.println("brother(s) ");





System.out.print ("I have ");


System.out.print (sis);


System.out.println("sister(s) ");





System.out.print ("I have ");


System.out.print (ftotal);


System.out.println("Members in my family ");





}


}





"test" program gets compiled but the "Sum" program don't even compile properly. i cant find any errors. when i type





C:\%26gt;Winura%26gt;javac Sum.java





or





C:\%26gt;Winura%26gt;javac Test.java





i don't get an out put. "Test" compiles but no output, "Sum" don't even compile. PLEASE HELP!

Need Help from someone who's familiar with programming...PLEASE!!!!?
When compiling a Java class, an output file is only produced if the file is compiled successfully. Compilation of the Sum class produces the following error for me:





/tmp/5045/Sum.java:5: non-static variable total cannot be referenced from a static context


System.out.println(total);


^


1 error





In this case the error is related to the keyword static. Your main() method is defined using the keyword static as it should be, but this means that the method is tied to the Sum class and not to an instance of that class (if you're completely new to Java this may not make sense, but have a look into Object Oriented (OO) Programming and I hope this will become clearer. A static method is limited to calling only static methods and accessing static variables.





To fix this error you must either:





Change the variables accessed within the main() method to be static using the keyword, e.g.





class House {


private static int windows = 4;





public static void main(String args[]) {


System.out.println("This house has " + windows + " windows.");


}


}





Or, you could move the variables into the static method. This works well for trivial programming examples but doesn't really mean much in a more complex situation. In the example below, a house no longer has four windows because the variable no longer belongs to the House class (or instances thereof) but simply to the main()method:





class House {


public static void main(String args[]) {


int windows = 4;


System.out.println("This house has " + windows + " windows.");


}


}





The final option is to create an instance of the class inside main() and then access its values. Without knowing OO Programming this might seem like the more difficult and least intuitive option but later in your programming career it will hopefully become obvious that this is a far better solution. An example of this would look like:





class House {


private int windows = 4;





public static void main(String args[]) {


House myHouse = new House();


System.out.println("This house has " + myHouse.windows + " windows.");


}


}





All three of the above examples produce the same output when compiled and run. To compile the file I type:





javac House.java





As no errors are produced, no text output is produced to my console but a new output file is produced called House.class. To run this new file (and so my application), I can then type:





java House





And the following output is produced:





This house has 4 windows.





----





Test.java does successfully compile which is why you get no error output. As with the House java file, typing javac for the Test file produces a new file Test.class.





To run the new Test class type the following command into your console:





java Test





For me running your code produces the following output:





My name is: W


I have 1brother(s)


I have 0sister(s)


I have 4Members in my family





Hope that helps.


No comments:

Post a Comment