You are on page 1of 2

Assignment-2

1. Write a main method to input a length in meters using constructors, convert this to feet
and inches, and write both lengths on the screen. 1 meter = 39.4 inches.

2. A diamond merchant has recently received a consignment of stones. The diamonds are to
be categorized by weight according to Figure 5.20. At the end of the weighing, the
merchant requires a print-out of the total number of stones in each category and the
percentage weight of each category. Assume that the electronic scale the merchant uses is
calibrated in milligrams. Write a program to input the weights of the diamonds and
output the required statistics. Note: 200 mg is equivalent to 1 carat (SI).

3. Write a program to store the alphabet as characters in an array. The program should
display:

a. The entire alphabet

b. The first six characters of the alphabet

c. The last 10 characters of the alphabet

d. The tenth character of the alphabet

4. Write a program to input 10 integers in numerical ascending order into a one-dimen-sional


array X; copy the numbers from array X to another one-dimensional array Y, such that array Y
contains the numbers in descending order. Output the contents of array Y.

5. Write a Survey class that will support taking surveys on various topics. The class should make
use of the CheckBox class for a survey in which a particular question that will posed, along with
a list of potential answers. The class should allow the programmer to easily define, administer,
and report the results of the survey.

Next write a program that uses the Survey class to survey a group of students about why
they are studying computer programming and Java.

6. Sometimes, the class can contain only the method declaration, without and body. And these
classes can also be used to implement multiple inheritances. What are these classes and explain
the same with suitable example.

7. Devise a class CharacterString that has the following methods.


class CharacterString
{
// constructor
public CharacterString(char[] value);
// instance methods
// return the length of a CharacterString
public int length();
// delete N characters from this string starting at the Ith
// character
public CharacterString delete(int N, int I);
// insert CharacterString A into this string, starting at
// position I
public CharacterString insert(CharacterString A, int I);
// remove substring A from this string
public CharacterString remove(CharacterString A);
// duplicate this string
public CharacterString duplicate();
// test this string with string A for equality
public boolean equals(CharacterString A);
// display this string
public void display();
}
Write a program to test all the methods of the class CharacterString.

8. Sometimes we want a class to inherit from more than one class. In Java how it can be
accomplished? Explain it briefly and also write a program to implement it.

You might also like