Question: qns 1: Write Java Code for the following case scenarios: Consider the following class declaration: public class Circle { private double radius; public Circle (double
qns 1: Write Java Code for the following case scenarios:
Consider the following class declaration:
public class Circle
{
private double radius;
public Circle (double r)
{
radius = r;
}
public double getArea()
{
return Math.PI * radius * radius;
}
public double getRadius()
{
return radius;
}
}
qns 2.Write a toString method for this class. The method should return a string containing the radius and area of the circle.
qns 3:Write an equals method for this class. The method should accept a Circle object as an argument. It should return true if the argument object contains the same data as the calling object, or false otherwise.
qns 4:Write a greaterThan method for this class. The method should accept a Circle object as an argument. It should return true if the argument object has an area that is greater than the area of the calling object, or false otherwise.
Make a Land Tract class that has two fields: one for the tracts length and one for the width. The class should have a method that returns the tracts area, as well as the equals method and a toString()method. Demonstrate the class in a program that asks the user to enter the dimensions for two tracts of land. The program should display the area of each tract of land and indicate whether the tracts are of equal size.
qns 5: Identify the output of following Java code. Justify the syntax.
File Name: Instuctor.java
/**
This class stores data about an instructor.
*/
public class Instructor
{
private String lastName; // Last name
private String firstName; // First name
private String officeNumber; // Office number
/**
This constructor initializes the last name,
first name, and office number.
@param lname The instructor's last name.
@param fname The instructor's first name.
@param office The office number.
*/
public Instructor(String lname, String fname,
String office)
{
lastName = lname;
firstName = fname;
officeNumber = office;
}
/**
The copy constructor initializes the object
as a copy of another Instructor object.
@param object2 The object to copy.
*/
public Instructor(Instructor object2)
{
lastName = object2.lastName;
firstName = object2.firstName;
officeNumber = object2.officeNumber;
}
/**
The set method sets a value for each field.
@param lname The instructor's last name.
@param fname The instructor's first name.
@param office The office number.
*/
public void set(String lname, String fname,
String office)
{
lastName = lname;
firstName = fname;
officeNumber = office;
}
/**
toString method
@return A string containing the instructor
information.
*/
public String toString()
{
// Create a string representing the object.
String str = "Last Name: " + lastName +
" First Name: " + firstName +
" Office Number: " + officeNumber;
// Return the string.
return str;
}
}
File Name: TextBook.java
/**
This class stores data about a textbook.
*/
public class TextBook
{
private String title; // Title of the book
private String author; // Author's last name
private String publisher; // Name of publisher
/**
This constructor initializes the title,
author, and publisher fields
@param textTitle The book's title.
@param auth The author's name.
@param pub The name of the publisher.
*/
public TextBook(String textTitle, String auth,
String pub)
{
title = textTitle;
author = auth;
publisher = pub;
}
/**
The copy constructor initializes the object
as a copy of another TextBook object.
@param object2 The object to copy.
*/
public TextBook(TextBook object2)
{
title = object2.title;
author = object2.author;
publisher = object2.publisher;
}
/**
The set method sets a value for each field.
@param textTitle The book's title.
@param auth The author's name.
@param pub The name of the publisher.
*/
public void set(String textTitle, String auth,
String pub)
{
title = textTitle;
author = auth;
publisher = pub;
}
/**
toString method
@return A string containing the textbook
information.
*/
public String toString()
{
// Create a string representing the object.
String str = "Title: " + title +
" Author: " + author +
" Publisher: " + publisher;
// Return the string.
return str;
}
}
File name: Course.java
/**
This class stores data about a course.
*/
public class Course
{
private String courseName; // Name of the course
private Instructor instructor; // The instructor
private TextBook textBook; // The textbook
/**
This constructor initializes the courseName,
instructor, and text fields.
@param name The name of the course.
@param instructor An Instructor object.
@param text A TextBook object.
*/
public Course(String name, Instructor instr,
TextBook text)
{
// Assign the courseName.
courseName = name;
// Create a new Instructor object, passing
// instr as an argument to the copy constructor.
instructor = new Instructor(instr);
// Create a new TextBook object, passing
// text as an argument to the copy constructor.
textBook = new TextBook(text);
}
/**
getName method
@return The name of the course.
*/
public String getName()
{
return courseName;
}
/**
getInstructor method
@return A reference to a copy of this course's
Instructor object.
*/
public Instructor getInstructor()
{
// Return a copy of the instructor object.
return new Instructor(instructor);
}
/**
getTextBook method
@return A reference to a copy of this course's
TextBook object.
*/
public TextBook getTextBook()
{
// Return a copy of the textBook object.
return new TextBook(textBook);
}
/**
toString method
@return A string containing the course information.
*/
public String toString()
{
// Create a string representing the object.
String str = "Course name: " + courseName +
" Instructor Information: " +
instructor +
" Textbook Information: " +
textBook;
// Return the string.
return str;
}
}
File name: CourseDemo.java
/**
This program demonstrates the Course class.
*/
public class CourseDemo
{
public static void main(String[] args)
{
// Create an Instructor object.
Instructor myInstructor =
new Instructor("Kramer", "Shawn", "RH3010");
// Create a TextBook object.
TextBook myTextBook =
new TextBook("Starting Out with Java",
"Gaddis", "Scott/Jones");
// Create a Course object.
Course myCourse =
new Course("Intro to Java", myInstructor,
myTextBook);
// Display the course information.
System.out.println(myCourse);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
