CORE JAVA – WORKING EXERCISES - 1 1.
The following is a set of seven sequentially related questions. a) Create a class called Message with a default constructor (one that takes no arguments). Print a default message in the constructor using System.out.println( ). (Don’t add any other fields / methods, and don’t specify any package.) b) Add an overloaded constructor to the Message class that takes a String argument and prints it along with your message (no need to format). c) Create a class called DisplayMessages (in a separate file in the same directory). In the main( ) function, create an array of type Message (the class you created in a above), having 5 elements, consisting of object references of the Message class. Don’t actually create objects to be assigned to these references yet. When you compile and run the program, are the initialization messages from the constructor calls printed? Why? d) Assign objects to the array of Message references through a loop, using the default no-argument constructor in Message. Run the program to see if the messages are printed. e) Comment out the code inserted in (d) above, and create the Message objects in the array again, this time using the second overloaded constructor in Message, to print five specific messages (without using a loop). Run the program again to see if the default and specific messages are printed. f) Finally, place these files into a folder structure called project \ messages anywhere in your file system. Make project.messages the package of both the classes. Execute the program by making appropriate changes to the classpath. Write the command that you used to execute the program, and the corresponding classpath entry. g) In the above exercise, if you changed the package for both the classes to just messages instead of project.messages, what change would occur in the classpath entry and the command for running the program?
2.
Create a class called “Statement”. It should contain
a) b) c) d)
A single field – the Statement ID Set and Get methods for the Statement ID field A constructor that takes no arguments and initializes the Statement ID to 10. Another constructor that takes a statement ID as argument and initializes the Statement ID field with that argument.
Derive two classes, both directly extended from “Statement”: I)
“FinancialStatement”. This class should contain a) A single field – the Financial Statement ID b) Set and Get methods for the Financial Statement ID field c) A constructor that takes one argument to initialize the Financial Statement ID field, but does not take any argument to initialize the base class Statement ID field.
II)
“EconomicStatement”. This class should contain a) A single field – the Economic Statement ID b) Set and Get methods for the Economic Statement ID field c) A constructor that takes two arguments – one to initialize the Economic Statement ID field, and another to explicitly initialize the base class Statement ID field.
Put all the classes in one file called InheritanceDemo.java. Write a public class called InheritanceDemo in the same file. In the main( ) method of that class, create a) A “FinancialStatement” object that initializes its financial statement ID to 100. Print the values of its base class statement ID and financial statement ID by calling the appropriate “Get” methods. b) An “EconomicStatement” object that initializes its economic statement ID to 200 and its base class statement ID to 20. Print the values of its base class statement ID and economic statement ID by calling the appropriate “Get” methods.
3.
Create a class called Rating.
a) b) c) d)
e) f)
4.
It should have a field for the Rating ID. It should have a field for the Rating Name It should have a method called setRatingDetails for setting both the rating ID and the rating name by taking in arguments for the ID and name. It should have an overloaded method by the same name setRatingDetails that sets the rating ID through an argument, and sets a default rating name as “General Rating”. It should have a display( ) method that prints the rating ID and the rating name. Write a main( ) function in the same Rating class that creates an object of class Rating. a. First set the rating details of the object by calling the method that takes both the ID and the name as arguments (pass anything as the ID and name). Display the values by calling the display( ) method. b. Now, re-set its rating details by calling the method that takes only the ID as argument. Display the values by calling the display( ) method.
Create a base class called DataStatement. It should just have three methods:
a) b) c)
void getRawData( ) void generateProcessedData( ) void createStatementReport( )
These methods should not have any implementation in the base class i.e. you can leave the methods empty or print some message. Now create three derived classes extending from this base class: i) ii) iii)
FinancialDataStatement EconomicDataStatement NationalDataStatement
Each of these classes should override all the three methods in the base class mentioned above, and provide a suitable specific implementation for each method. (For the implementation of the methods you can put some specific System.out.println statements, specific to the class out of the three classes to which the method belongs) Create a public class called ProcessDataStatement. It should contain a method called processStatement( ). This method should accept a reference variable of DataStatement class, and call the three methods getRawData( ), generateProcessedData( ) and createStatementReport( ) on the variable in succession. In the main( ) method of the ProcessDataStatement class, create three objects of type FinancialDataStatement, EconomicDataStatement and NationalDataStatement respectively. For each of these objects, call the same processStatement( ) method by passing in the object as the argument. In the output of the program, note if the processStatement( ) method dynamically changes its behavior based on the type of object passed to it. Put all the classes that you create in the same file ProcessDataStatement.java, for ease of execution.
5.
Implement the previous exercise again by changing the base class DataStatement to an interface, and make the three derived classes implement the interface. (The
remaining code will remain almost the same). Code this example in a separate java file.
6.
Write a program called DataConversionDemo. In the main( ) function, read three arguments from the command line. Provide the values from the command line as follows: 1st argument: 2nd argument: 3rd argument:
10 34.2 35.2
( i.e. run the program as java DataConversionDemo 10 34.2 35.2 ) In the main( ) function, i) ii) iii)
Convert the first argument into an int primitive type, the second into a double primitive type, and the third into a double primitive type. Add them and put the result in a double primitive type variable. Convert the result back to a String and print it out.