[ Pobierz całość w formacie PDF ]
.To be able to sum those integers, you haveto convert them back from strings to integers.There's a classmethod for the Integer class,called parseInt, that doesjust this.If you change line 6 to use that method, everythingworks just fine:sum += Integer.parseInt(args[i]);Now, compiling the program produces no errors and running it withvarious arguments returns the expected results.For example, javaSumAverage 1 2 3 returns the following output:Sum is: 6Average is: 2SummaryToday you put together everything you've come across in the precedingdays of this week about how to create Java classes and use themin Java applications.This includes the following:Instance and class variables, which hold the attributes ofthe class and its instances.You have learned how to declare them,how they are different from regular local variables, and how todeclare constants.Instance and class methods, which define a class's behavior.You have learned how to define methods, including the parts ofa method's signature, how to return values from a method, howarguments are passed in and out of methods, and how to use thethis keyword to refer tothe current object.Java applications-all about the main()method and how it works, as well as how to pass arguments intoa Java application from a command line.Q&AQ:I tried creating a constant variable inside a method, and I got a compiler error when I tried it.What was I doing wrong?A:You can create only constant (final) class or instance variables; local variables cannot be constant.Q:static and final are not exactly the most descriptive words for creating class variables, class methods, and constants.Why not use class and const?A:static comes from Java's C++ heritage; C++ uses the static keyword to retain memory for class variables and methods (and, in fact, they aren't called class methods and variables in C++: static member functions and variables are more common terms).final, however, is new.final is used in a more general way for classes and methods to indicate that those things cannot be subclassed or overridden.Using the final keyword for variables is consistent with that behavior.final variables are not quite the same as constant variables in C++, which is why the const keyword is not used.Q:In my class, I have an instance variable called origin.I also have a local variable called origin in a method, which, because of variable scope, gets hidden by the local variable.Is there any way to get hold of the instance variable's value?A:The easiest way is not to name your local variables the same names as your instance variables.If you feel you must, you can use this.origin to refer to the instance variable and origin to refer to the local variable.Q:I want to pass command-line arguments to an applet.How do I do this?A:You're writing applets already? Been skipping ahead, have you? The answer is that you use HTML attributes to pass arguments to an applet, not the command line (you don't have a command line for applets).You'll learn how to do this next week.Q:I wrote a program to take four arguments, but if I give it too few arguments, it crashes with a runtime error.A:Testing for the number and type of arguments your program expects is up to you in your Java program; Java won't do it for you.If your program requires four arguments, test that you have indeed been given four arguments, and return an error message if you haven't.Use of this site is subject to certainCopyright (c) 1996-1998EarthWeb, Inc.All rights reserved.Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited
[ Pobierz całość w formacie PDF ]