Professional Documents
Culture Documents
Note:
- The assignment needs to be submitted by 15 Apr 2015, to your respective faculty.
-9(b) can be submitted with the second assignment.
Question
1.
3.
4.
5.
Who am I ?
a) I am compiled from a .java file.
b) My instance variable values can
be different from my buddys
values.
c) I behave like a template.
d) I like to do stuff.
e) I can have many methods.
f) I represent state.
g) I have behaviors.
h) I am located in objects.
i) I live on the heap.
j) I am used to create object instances.
k) My state can change.
l) I declare methods.
m) I can change at runtime.
class
What happens if the argument you want to pass is an object Instead of primitive? Give example
Fix the code
class XCopy
{
public static void main(String [] args)
{
class Clock
{
String time;
Dint orig = 42;
void setTime(String t)
time = t;
Xcopy x = new xCopy();
int y = x.go(orig);
System.out.println(orig + + y);
void getTime()
return time;
}
int go(int arg)
{arg = arg * 2;}
class ClockTestDrive {
public static void main(String [] args) {
return arg;
}
Clock c = new Clock();
}
c.setTime(U124Sn ) ;
String tad = c.getTime();
System.out.println(time: + tad);
6.
Whats wrong with the following program?Fix the problem
public class SomethingIsWrong {
public static void main(String[] args) {
Rectangle myRect;
myRect.width = 40;
myRect.height = 50;
System.out.println(myRects area is + myRect.area());
}
}
7.
The following code creates one array and one string object. How many references to those objects
exist after the code executes? Is either object eligible for garbage collection?
Given the following class, called Number Holder, write some code that creates an instance of the
class, initializes its two member variables, and then displays the value of each member variable.
Public class NumberHolder {
public int anInt;
public float aFloat;
}
9.
a) Write a Java application that will execute the following main method and display a date in the
format "month/day/year" syntax followed by your name.
b) Implement an abstract class named Person and two subclasses named Student and Employee in
Java. A person has a name, address, phone number and e-mail address. A student has a class status
(freshman, sophomore, junior or senior). Define the status as a constant. An employee has an office,
salary and date-hired. Implement the above classes in Java. Provide Constructors for classes to
initialize private variables. Override the toString method in each class to display the class name and
the persons name. Write an application to create objects of type Student and Employee and print the
persons name and the class name of the objects.
c) A library loans three different kinds of items to customers: books, video tapes and compact disks.
Each item has a title, and publisher. In addition, books have an author, and CDs have an artist. The
library may have multiple copies of the same book, video tape or compact disk. There are two
different kinds of customer: students and staff. For both kinds of customer, the library has their name,
id and address. Students may borrow at most 20 items. Write an application named library.