JAVA Homework

profilebeoto01
java_Homework1.doc

1. A (5 points) Write a Java program that:

· Asks for an integer value n;

· Then asks for n number(s) from the user;

· Finally displays the average of all the numbers typed in by the user.

B (5 points) Write a program segment in Java that sets the value of “int my_Number” randomly from the following set: {2, 4, 6, 60, 62}.

C (5 points) Write a method Power(base, exponent) that returns the value of

image1.wmf

onent

base

exp

;

(Assume that base is a positive, non-zero double and exponent is a double.)

2. A (4 points)Explain autoboxing and auto-unboxing in Java and give an example for each.

B (8 points)Write a program that rolls a FOUR -sided die 20 times and stores results in ascending order in int a[20].

C (4 points)What does the following program print? Why? Explain with your own words.

public class Program

{

public static void main (String [] args)

{

final int arraySize =5;

int a[] = { 1, 2, 3, 4, 5 };

int result = whatIsThis(a, arraySize);

System.out.printf("Result is : %d.\n", result);

}

public static int whatIsThis(int b[], int size)

{

if ( size == 1 )

return b[ 0 ];

else

return 6 * (b[ size - 1 ] ) + whatIsThis(b, size - 1 );

}

}

3. A (12 points) (Complex Numbers) Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form

realPart + imaginaryPart * i

where i is

image2.wmf

1

-

Write a program to test your class. Use floating-point variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform the following operations:

a) Add two Complex numbers: the real parts are added together and the imaginary parts are added together.

Hint:

For this exercise, please consider the use of this reference (see from subchapter 8.4).

A template looks like:

// Add two Complex numbers

public Complex add( Complex Right )

{

/* Write code here */

}

b) Subtract two Complex numbers: the real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.

c) Print Complex numbers in the form (a, b), where a is the real part and b is the imaginary part.

4. A (4 points) What is a subclass ? Can a subclass access the members of its superclass? If yes, how? If not, why? Explain.

B (4 points) A method can access the superclass’s private members, but it can not access the subclass private members. What can be inferred about the method?

C (8 points) Consider class Point provided in APPENDIX A (1 file).

Inherit class Square. A Square is just a Point and a side. It should include methods for getting and setting the side as well as calculating the area.

Based on class Square, inherit class Cube. A Cube is a Square extended into three dimensions. It requires no additional data members.

Redefine the calculation of the area to calculate the surface area and add a member method to calculate the volume. The surface area can be calculated with the formula side * side * 6, and the volume may be calculated with the formula side * side * side.

Finally, write a short driver program to show the features of your code.

image3.emf

Lower Left

Point

Side

Side

image4.emf

Lower Left

Point

Side

Side

S

i

d

e

5. A (16 points) Construct the class of non-negative 3x3 Matrices containing non-negative double elements with the following functionalities:

image5.wmf

÷

÷

÷

ø

ö

ç

ç

ç

è

æ

=

33

32

31

23

22

21

13

12

11

a

a

a

a

a

a

a

a

a

A

;

· Determinant method returns the double value of the matrix determinant:

image6.wmf

)

(

)

(

)

(

)

det(

31

22

32

21

13

33

21

31

23

12

32

23

33

22

11

a

a

a

a

a

a

a

a

a

a

a

a

a

a

a

A

*

-

*

*

+

*

-

*

*

+

*

-

*

*

=

· isNull method returns true if the matrix is a Null Matrix (all the elements are 0). Otherwise, isNull method returns false.

· isUMatrix method returns true if the matrix is a U Matrix. Otherwise the return value is false. Let

image7.wmf

÷

÷

÷

ø

ö

ç

ç

ç

è

æ

=

1

1

1

1

1

1

1

1

1

U

;

· Multiplication results in a 3x3 matrix and takes a constant of double type as the only input parameter. The resulting matrix R is:

image8.wmf

÷

÷

÷

ø

ö

ç

ç

ç

è

æ

*

*

*

*

*

*

*

*

*

=

*

=

33

32

31

23

22

21

13

12

11

a

d

a

d

a

d

a

d

a

d

a

d

a

d

a

d

a

d

A

d

R

;

· Addition method returns a 3x3 sum of two matrices (it takes the addend matrix as an input parameter). The resulting matrix R is:

image9.wmf

;

33

33

32

32

31

31

23

23

22

22

21

21

13

13

12

12

11

11

33

32

31

23

22

21

13

12

11

33

32

31

23

22

21

13

12

11

÷

÷

÷

ø

ö

ç

ç

ç

è

æ

+

+

+

+

+

+

+

+

+

=

÷

÷

÷

ø

ö

ç

ç

ç

è

æ

+

÷

÷

÷

ø

ö

ç

ç

ç

è

æ

=

+

=

b

a

b

a

b

a

b

a

b

a

b

a

b

a

b

a

b

a

b

b

b

b

b

b

b

b

b

a

a

a

a

a

a

a

a

a

B

A

R

Implement the member methods of class of 3x3 matrices with the functionalities above.

B (5 points) Given matrix X and matrix Y. Write a program segment that instantiates matrix Z and sets its elements:

image10.wmf

Y

X

Z

+

*

=

2

;

6. A (10 points) Write a program that creates a queue of integers. After that it loads in 20 random integers between 1 and 6, and after the 20th number it prints out the content of the queue. Finally, the program calculates and prints out the average of the values without losing the values from the queue!

(For the queue structure, consider Chapter 21, Data Structures.)

7. A (10 points) Create a class of Circle with attributes radius, X and Y that represent the Cartesian coordinates of the center point. Radius default value is 1, it must be positive for every circle. Provide a member method Cross that returns true if two circles cross each other. Otherwise it returns false. Cross has a Circle object input parameter.

image11.emf

radius1

radius2

C1

(X1,Y1)

C2

(X2,Y2)

APPENDIX A – For Problem 4.C

// Point.java

// Class Point definition

public class Point

{

private double x; // x coordinate

private double y; // y coordinate

// two-argument constructor

public Point( double xCoordinate, double yCoordinate )

{

x = xCoordinate; // set x

y = yCoordinate; // set y

} // end two-argument Point constructor

// return x

public double getX()

{

return x;

} // end method getX

// return y

public double getY()

{

return y;

} // end method getY

// return string representation of Point object

public String toString()

{

return String.format( "( %.1f, %.1f )", getX(), getY() );

} // end method toString

} // end class Point

_1175965051.unknown

_1184414212.unknown

_1184567242.vsd

Lower Left Point

Side

Side

_1184567273.vsd

Lower Left Point

Side

Side

Side

_1175965216.unknown

_1175965607.vsd

radius1

radius2

C1 (X1,Y1)

C2 (X2,Y2)

_1163785606.unknown

_1163786128.unknown

_1163787074.unknown

_1163785848.unknown

_1163781655.unknown