java assignment 1

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... “); } } 2 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?

Upload: daman-toor

Post on 15-Jul-2015

1.050 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Java assignment 1

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... “);

}

}

2 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?

Page 2: Java assignment 1

3. Who am I ?

a) I am compiled from a .java file. class

b) My instance variable values can

be different from my buddy’s

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.

4. What happens if the argument you want to pass is an object Instead of primitive? Give example

5. Fix the code

class XCopy {

public static void main(Strinq [) arqs) {

class Clock {

String time;

2nto rig = 42;

void setTime(String t)

time = tj

Xcopy x = new xCopy();

int y : x.go(orig)j

System.out.println(orig + U U + 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 ) j

String tad = c.getTime()i

System.out.println(Utime: u + tod)j

6. What’s 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(“myRect’s area is “ + myRect.area());

}

}

7. The following code creates one array and one string object. How many references to those objects

Page 3: Java assignment 1

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;

8 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 person’s name. Write an application to create objects of type Student and Employee and print the

person’s 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.