exercises for ja se

21
Exercises for Week 1

Upload: sshhzap

Post on 25-May-2015

352 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Exercises for ja se

Exercises for Week 1

Page 2: Exercises for ja se

Public static void main (String args[])

• The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared.In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started. The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made. The keyword void simply tells the compiler that main( ) does not return a value. As you will see, methods may also return values. As stated, main( ) is the method called when a Java application begins. Keep in mind that Java is case-sensitive. Thus, Main is different from main. It is important to understand that the Java compiler will compile classes that do not contain a main( ) method. But the Java interpreter has no way to run these classes. So, if you had typed Main instead of main, the compiler would still compile your program. However, the Java interpreter would report an error because it would be unable to find the main( ) method. Any information that you need to pass to a method is received by variables specified within the set of parentheses that follow the name of the method. These variables are called parameters. If there are no parameters required for a given method, you still need to include the empty parentheses. In main( ), there is only one parameter, albeit a complicated one. String args[ ] declares a parameter named args, which is an array of instances of the class String. Objects of type String store character strings. In this case, args receives any command-line arguments present when the program is executed.

Page 3: Exercises for ja se

Class BufferedReader• http://java.sun.com/j2se/1.4.2/docs/api/java/io/BufferedReader.html • Read text from a character-input stream, buffering characters so as

to provide for the efficient reading of characters, arrays, and lines.The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as File Readers and InputStreamReaders. For example,BufferedReader in = new BufferedReader(new FileReader("foo.in")); will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient. Programs that use DataInputStreams for textual input can be localized by replacing each DataInputStream with an appropriate BufferedReader.

Page 4: Exercises for ja se

Class InputStreamReader

• An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.For top efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example:BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

Page 5: Exercises for ja se

NOW TRY TO CREATE YOR OWN SLAM BOOK HAVING 15 QUESTIONS

Page 6: Exercises for ja se

CREATE A SIMPLE CALCULATOR THAT CAN PERFORM + - / * AND %

Page 7: Exercises for ja se

CREATE A PROGRAM TEPMPERATURE CONVERSION PROGRAM

Page 8: Exercises for ja se
Page 9: Exercises for ja se

WRITE AN ARRAY LIST THAT DIPLAY ALL THE MONTHS OF THE YEAR

Page 10: Exercises for ja se
Page 11: Exercises for ja se

WRITE A MESSAGE OR ANY COMMENT ABOUT YOUR INSTRUCTOR

Page 12: Exercises for ja se

DENOMINATION COUNTER PART1

Page 13: Exercises for ja se

DENOMINATION COUNTERPART 2

Page 14: Exercises for ja se

DENOMINATION COUNTER PART3

Page 15: Exercises for ja se
Page 16: Exercises for ja se

Create a program that will compute for the weight of an object weight (kg) equal to mass multiplied by gravitational pull of the earth(9.8 meter/sec).

Page 17: Exercises for ja se
Page 18: Exercises for ja se
Page 19: Exercises for ja se

FOOD FOR THE BRAIN MESSAGE DIALOG TYPE ICON DESCRIPTION

JOptionPane.ERROR_MESSAGE YES Error message display

JOptionPane.INFORMATION_MESSAGE YES Information display

JOptionPane.WARNING_MESSAGE YES Warning display

JOptionPane.QUESTION_MESSAGE YES Display Questions

JOptionPane.PLAIN_MESSAGE YES Displays a message with no icons

Page 20: Exercises for ja se
Page 21: Exercises for ja se