Programming Questions and Exercises : Simple Class

Programming Questions and Exercises : Simple Class

Question 1
Consider the following definition of the class Sample:

public class Sample 
{
    private int x;
    private double y;

    public Sample() 
    {
    }

    public Sample(int a, double b)
    {
    }

    public void setSample()
    {
    }

    public void print()
    {
    }
}
a. How many constructors does class Sample have?
b. Write the definition of the member setSample so that x is set to 10 and y is set to 25.8.
c. Write the definition of the member print that prints the contents of x and y.
d. Write the definition of the default constructor of the class Sample so that the instance variables are initialized to 0.
e. Write the definition of the constructor with parameters of the class Sample so that the instance variable x is initialized to the value of a and the instance variable y is initialized to the value of b.
f. Write a Java statement that creates the Sample object s and initializes the instance variables of s to 20 and 35.0, respectively.
g. Write a Java statement that prints the values of the instance variables of s.
Question 2
Create a Java class to represent a Circle type, which should have the following :
Radius. Instance field of type double
No-argument constructor. Set radius with default value of 1.0
Constructor. that accept an argument for radius
getRadius. public method that returns the radius of Circle
getArea. Method that returns the area of Circle
Question 3
Create a Java class to represent a Pet type, which should have the following fields:
name. Instance variable of type String that holds the name of a pet.
animal. Instance variable of type String that holds the type of animal that a pet is.
age. Instance variable of type int holds the pet's age.
The Pet class should also have the following methods:
constructor for this class. The constructor should accept an argument for each of the fields.
setName, The setName method stores a value in the name field.
setAnimal. The setAnimal method stores a value in the animal field.
setAge. The setAge method stores a value in the age field.
getName. The getName method returns the value of the name field.
getAnimal. The getAnimal method returns the value of the animal field.
getAge. The getAge method returns the value of the age field.
Question 4
Create a new Java class named Car that has the following fields
year - The year field is an int that holds a car's year model (e.g. 2010)
make - The make field is a String object that holds the make of the car (e.g. "Porsche")
speed - The speed field is an double that holds a car's maximum speed (e.g. 25.0)
In addition, the Car class should have the following methods.
Constructor - The constructor should accept the car's year, make, and beginning speed as arguments
These values should be used to initialize the Car's year, make, and speed fields
Getter Methods - Write three accessor (getter) methods to get the values stored in an object's fields
getYear(), getMake(), getSpeed()



No comments:

Post a Comment

Pages