Pages

Programming Questions and Exercises : On Class Constructor

Question 1
Circle ClassCreate a class to represent a Circle type in java, which should have 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

public class Circle
{
    private double radius;

    public Circle()
    {
        radius = 1.0;
    }

    public Circle(double radius)
    {
        this.radius = radius;
    }

    public void setRadius(double radius)
    {
        this.radius = radius;
    }

    public double getRadius()
    {
        return radius;
    }
}

Question 2
Pet ClassCreate a class to represent a Pet type in java, 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.

public class Pet
{
    private String name;
    private String animal;
    private int age;

    public Pet(String name, String animal, int age)
    {
        this.name = name;
        this.animal = animal;
        this.age = age;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getAnimal()
    {
        return animal;
    }

    public void setAnimal(String animal)
    {
        this.animal = animal;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }
}

Question 3
Car ClassCreate a new class named Car that has the following fields:
year - The year field is an int that holds a car's year model (e.g. 2015)
make - The make field is a String object that holds the make of the car (e.g. "Honda")
speed - The speed field is an double that holds a car's maximum speed (e.g. 85.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()

public class Car
{
    private int year;
    private String make;
    private double speed;

    public Car(int year, String make, double speed)
    {
        this.year = year;
        this.make = make;
        this.speed = speed;
    }

    public int getYear()
    {
        return year;
    }

    public String getMake()
    {
        return make;
    }

    public double getSpeed()
    {
        return speed;
    }
}

Question 4
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.
public void setSample()
{
    x = 10;
    y = 25.8;
}================================================================================
c. Write the definition of the member print that prints the contents of x and y.
public void print()
{
    System.out.println("x = " + x + "y = " + y);
}
 d. Write the definition of the default constructor of the class Sample so that the instance variables are initialized to 0.
public Sample() 
{
    x = 0;
    y = 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.
public Sample(int a, double b) 
{
    x = a;
    y = 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.
Sample s = new Sample(20, 35.0);
 g. Write a Java statement that prints the values of the instance variables of s.
s.print();

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.