You are on page 1of 3

Java Assignment 1

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.

Fill the blanks:


public class EchoTestDrive {
public static void main(String [] args) {
Echo e1 = new Echo();
_________________________
int x = 0;
while ( ___________ ) {
e1.hello();
__________________________
if ( ____________ ) {
e2.count = e2.count + 1;
}
if ( ____________ ) {
e2.count = e2.count + e1.count;
}
x = x + 1;
}
System.out.println(e2.count);
}
}
class ____________ {
int _________ = 0;
void ___________ {
System.out.println(helloooo... );
}
}
a) Why does everything have to be in a class?
b) Do I have to put a main in every class I write?
c) How big is a reference variable? So, does that mean that all object references are the same
size, regardless of the size of the actual objects to which they refer?
d) Can I do arithmetic on a reference variable, Increment It you know Cstuff?

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?

String[] students = new String[10];


String studentName = Peter Parker;
students[0] = studentName;
studentName = null;

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.

You might also like