c) 10/25: Review the example from class: RollingDice which uses Die. RollingDice
ID: 3601244 • Letter: C
Question
c) 10/25: Review the example from class: RollingDice which uses Die. RollingDice is a driver class whose main method instantiates (i.e. creates) 2 Die objects, then calls the computeSum method which is defined in the Die class. As you review this example, let me know if something is not clear. Define 2 classes (in 2 separate files): 1) a driver class named TestPoint and 2) a blueprint class named Point: TestPoint is a driver class, whose main method instantiates 4 Point objects (e.g. Point p1 = new Point (0, 0); Point p2 = new Point (4, 3); Point p3 = new Point (3, 4); Point p4 = new Point (0, 0); ), then computes and prints the distance between the first 2 points and the distance between the last 2 points (by calling a method calcDistance as follows: distance1 = p1.calcDistance (p2); distance2 = p3.calcDistance (p4); where p1, p2, p3 and p4 are Point objects, and calcDistance is a method to be defined in the Point class). A Point contains instance data for the x and y coordinates. Define the Point constructor to accept and initialize this data. Include a method to calculate and return the distance from this point to another point. Try writing your driver code first, and writing comments before code. Include a toString() method to return a nicely-formatted String representation of a point, e.g. if a point's x value is 3 and y value is 4, then the toString() method should return the following String: "(3, 4)"; the toString method is defined in your Point class and called (invoked) in your TestPoint class in order to print the output in a readable way, e.g.: The distance between (0, 0) and (3, 4) is 5. d) Try writing your driver code first, and writing comments before code. Create two classes, Kennel and Dog, as follows. Kennel is a driver class, whose main method instantiates several Dog objects, then computes and prints their ages in "person years". A Dog contains instance data that represents the dog's name and age. Define the Dog constructor to accept and initialize instance data. Include a method to compute and return the age of the dog in "person years" (seven times the dog's age). Include a toString method that returns a one-line description of the dog. Include getter (accessor) and setter (mutator) methods for the name and age, i.e. public String getName() public double getAge() public void setName(String) public void setAge(double) c) 10/25: Review the example from class: RollingDice which uses Die. RollingDice is a driver class whose main method instantiates (i.e. creates) 2 Die objects, then calls the computeSum method which is defined in the Die class. As you review this example, let me know if something is not clear. Define 2 classes (in 2 separate files): 1) a driver class named TestPoint and 2) a blueprint class named Point: TestPoint is a driver class, whose main method instantiates 4 Point objects (e.g. Point p1 = new Point (0, 0); Point p2 = new Point (4, 3); Point p3 = new Point (3, 4); Point p4 = new Point (0, 0); ), then computes and prints the distance between the first 2 points and the distance between the last 2 points (by calling a method calcDistance as follows: distance1 = p1.calcDistance (p2); distance2 = p3.calcDistance (p4); where p1, p2, p3 and p4 are Point objects, and calcDistance is a method to be defined in the Point class). A Point contains instance data for the x and y coordinates. Define the Point constructor to accept and initialize this data. Include a method to calculate and return the distance from this point to another point. Try writing your driver code first, and writing comments before code. Include a toString() method to return a nicely-formatted String representation of a point, e.g. if a point's x value is 3 and y value is 4, then the toString() method should return the following String: "(3, 4)"; the toString method is defined in your Point class and called (invoked) in your TestPoint class in order to print the output in a readable way, e.g.: The distance between (0, 0) and (3, 4) is 5. d) Try writing your driver code first, and writing comments before code. Create two classes, Kennel and Dog, as follows. Kennel is a driver class, whose main method instantiates several Dog objects, then computes and prints their ages in "person years". A Dog contains instance data that represents the dog's name and age. Define the Dog constructor to accept and initialize instance data. Include a method to compute and return the age of the dog in "person years" (seven times the dog's age). Include a toString method that returns a one-line description of the dog. Include getter (accessor) and setter (mutator) methods for the name and age, i.e. public String getName() public double getAge() public void setName(String) public void setAge(double) c) 10/25: Review the example from class: RollingDice which uses Die. RollingDice is a driver class whose main method instantiates (i.e. creates) 2 Die objects, then calls the computeSum method which is defined in the Die class. As you review this example, let me know if something is not clear. Define 2 classes (in 2 separate files): 1) a driver class named TestPoint and 2) a blueprint class named Point: TestPoint is a driver class, whose main method instantiates 4 Point objects (e.g. Point p1 = new Point (0, 0); Point p2 = new Point (4, 3); Point p3 = new Point (3, 4); Point p4 = new Point (0, 0); ), then computes and prints the distance between the first 2 points and the distance between the last 2 points (by calling a method calcDistance as follows: distance1 = p1.calcDistance (p2); distance2 = p3.calcDistance (p4); where p1, p2, p3 and p4 are Point objects, and calcDistance is a method to be defined in the Point class). A Point contains instance data for the x and y coordinates. Define the Point constructor to accept and initialize this data. Include a method to calculate and return the distance from this point to another point. Try writing your driver code first, and writing comments before code. Include a toString() method to return a nicely-formatted String representation of a point, e.g. if a point's x value is 3 and y value is 4, then the toString() method should return the following String: "(3, 4)"; the toString method is defined in your Point class and called (invoked) in your TestPoint class in order to print the output in a readable way, e.g.: The distance between (0, 0) and (3, 4) is 5. d) Try writing your driver code first, and writing comments before code. Create two classes, Kennel and Dog, as follows. Kennel is a driver class, whose main method instantiates several Dog objects, then computes and prints their ages in "person years". A Dog contains instance data that represents the dog's name and age. Define the Dog constructor to accept and initialize instance data. Include a method to compute and return the age of the dog in "person years" (seven times the dog's age). Include a toString method that returns a one-line description of the dog. Include getter (accessor) and setter (mutator) methods for the name and age, i.e. public String getName() public double getAge() public void setName(String) public void setAge(double)Explanation / Answer
Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.
______________
Die.java
import java.util.Random;
public class Die {
// Declaring variables
private int num;
// Zero argumented constructor
public Die() {
// Creating an object of Random class Object
Random r = new Random();
// Getting random value for dice 1
num = r.nextInt(6) + 1;
}
//Setters and getters
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
// This method displays the randomly generated dice value
public void computeSum(Die d) {
System.out.println("Sum :" + (num + d.getNum()));
}
}
_______________
RollingDice.java
public class RollingDice {
public static void main(String[] args) {
//Creating two Die class objects
Die d1 = new Die();
Die d2 = new Die();
//calling the method on the Die class
d1.computeSum(d2);
}
}
________________
Output:
Sum :9
_______________
Point.java
public class Point {
// Declaring instance variables
private int x;
private int y;
// Zero argumented constructor
public Point() {
super();
}
// Parameterized constructor
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
// getters and setters
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
// This method will calculate the distance between two points
public double calcDistance(Point p) {
return Math.sqrt(Math.pow((p.x - x), 2) + Math.pow((p.y - y), 2));
}
// toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
___________________
TestPoint.java
public class TestPoint {
public static void main(String[] args) {
//Creating an instances of Point class
Point p1 = new Point(0, 0);
Point p2 = new Point(4, 3);
Point p3 = new Point(3, 4);
Point p4 = new Point(0, 0);
//Calculate the Distance between p1 and p2
System.out.println("The distance between " + p1 + " and " + p2 + " is " + p1.calcDistance(p2));
//Calculate the Distance between p3 and p4
System.out.println("The distance between " + p3 + " and " + p4 + " is " + p3.calcDistance(p4));
}
}
__________________
Output:
The distance between (0,0) and (4,3) is 5.0
The distance between (3,4) and (0,0) is 5.0
___________________
d)
Dog.java
public class Dog {
//Declaring instance variables
private String name;
private double age;
//Parameterized constructor
public Dog(String name, double age) {
super();
this.name = name;
this.age = age;
}
//getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
public double computeAge() {
return 7 * age;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Name=" + name + ", Age=" + age;
}
}
_________________
Kennel.java
import java.util.Scanner;
public class Kennel {
public static void main(String[] args) {
// Declaring variables
String name;
double age;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
// Getting the input entered by the user
System.out.println("** Dog#1 **");
System.out.print("Enter the name :");
name = sc.nextLine();
System.out.print("Enter the age :");
age = sc.nextDouble();
sc.nextLine();
// Creating an instance of Dog class
Dog d1 = new Dog(name, age);
// Getting the input entered by the user
System.out.println("** Dog#2 **");
System.out.print("Enter the name :");
name = sc.nextLine();
System.out.print("Enter the age :");
age = sc.nextDouble();
// Creating an instance of Dog class
Dog d2 = new Dog(name, age);
// Computing the human years age
System.out.println("Age in Human Years of Dog#1:" + d1.computeAge());
System.out.println("Age in Human Years of Dog#2:" + d2.computeAge());
// Displaying the Dog class info
System.out.println("Dog#1 info:" + d1);
System.out.println("Dog#2 info:" + d2);
}
}
_________________
Output:
** Dog#1 **
Enter the name :Max
Enter the age :2
** Dog#2 **
Enter the name :Micky
Enter the age :3
Age in Human Years of Dog#1:14.0
Age in Human Years of Dog#2:21.0
Dog#1 info:Name=Max, Age=2.0
Dog#2 info:Name=Micky, Age=3.0
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.