Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 1: Write a Java class to model a Ball. A ball has a color and radius (es

ID: 3876895 • Letter: P

Question

Problem 1:

Write a Java class to model a Ball. A ball has a color and radius (established initially when a ball object is created). Include getter and setter methods, a toString method, an equals method (balls are equal if they have the same radius and color) and a compareTo method (based on radius alone). The Ball class should include methods to compute (and return) the ball’s circumference, surface area and volume. It should also include a static variable to store a count of the number of Ball objects created. Write a test class to create a few balls and test their methods. Make sure you print a count of the number of balls created (using the static variable).

Problem 2:

Design a Message class to represent an e-mail message. A message has a recipient, a sender and message text. Your class should have the following methods:

A constructor that accepts the sender and recipient

An append method that appends a line of text to the message body

A toString method that turns the message into a long string such as “From: Luke

Skywalker%nTo: Darth Vader%nText: Are you sure you’re my father?”

Then design a Mailbox class to store e-mail messages. This class should support the following methods:

public void addMessage(Message m)

public Message getMessage(int i)

public void removeMessage(int i)

Write a simple menu-driven application to test these classes. It should provide these menu options:

New message - prompts the user to enter all message information
Delete message - prompts for the message index and deletes that message View message - prompts for the message index and then displays the message List messages - displays all messages
Quit

Note: Each class should be in its own file. All classes should be contained in one BlueJ project.

Explanation / Answer

1.

public class Ball {

   private String colour;

   private double radius;

   public static int ballCount;

   /**

   * @param colour

   * @param radius

   */

   public Ball(String colour, double radius) {

       super();

       ballCount = ballCount+1;

       this.colour = colour;

       this.radius = radius;

   }

   /**

   * @return the colour

   */

   public String getColour() {

       return colour;

   }

   /**

   * @param colour the colour to set

   */

   public void setColour(String colour) {

       this.colour = colour;

   }

   /**

   * @return the radius

   */

   public double getRadius() {

       return radius;

   }

   /**

   * @param radius the radius to set

   */

   public void setRadius(double radius) {

       this.radius = radius;

   }

   /* toString(): return String

   */

   @Override

   public String toString() {

       return "Ball [colour=" + colour + ", radius=" + radius + "]";

   }

  

   /* equals return true if ball is equal

   */

   public boolean equals(Ball obj) {

       if (this == obj)

           return true;

       if (obj == null)

           return false;

       if (colour == null) {

           if (obj.colour != null)

               return false;

       } else if (!colour.equalsIgnoreCase(obj.colour))

           return false;

       if (Double.doubleToLongBits(radius) != Double.doubleToLongBits(obj.radius))

           return false;

       return true;

   }

   //compare ball radius and return the difference of radius

   public double compareTo(Ball obj){

       if (radius == obj.radius)

               return 0;

       else

           return radius - obj.radius;

   }

   //return the circumference of ball

   public double computeCircumference(){

       return 6.28*radius;

   }

   //return the surface area of ball

   public double computeSurfaceArea(){

       return 12.56*radius*radius;

   }

   //return the volume of ball

   public double computeVolume(){

       return 4.187*radius*radius*radius;

   }

  

}

---------------------------------------------------------

public class TestBall {

   public TestBall() {

   }

   public static void main(String[] args) {

       Ball ball1 = new Ball("white",2);

       Ball ball2 = new Ball("white",2);

       Ball ball3 = new Ball("Black",5);

       System.out.println("The count of ball created"+Ball.ballCount);

       System.out.println("Circumference of ball1 = " + ball1.computeCircumference());

       System.out.println("Surface Area of ball2 = "+ball2.computeSurfaceArea());

       System.out.println("Volume of ball3 = "+ball3.computeVolume());

       if(ball1.equals(ball2))

           System.out.println(" ball1 and ball2 are same");

       else

           System.out.println("Difference in radius of ball1 and ball2"+ball1.compareTo(ball2));

       if(ball1.equals(ball3))

           System.out.println(" ball1 and ball2 are same");

       else

           System.out.println("Difference in radius of ball1 and ball3"+ball1.compareTo(ball3));

      

       System.out.println("Balls are: "+ ball1+" "+ball2+" "+ball3);

   }

}

----------------------------------------------------

Sample Output:

The count of ball created3

Circumference of ball1 = 12.56

Surface Area of ball2 = 50.24

Volume of ball3 = 523.375

ball1 and ball2 are same

Difference in radius of ball1 and ball3-3.0

Balls are:

Ball [colour=white, radius=2.0]

Ball [colour=white, radius=2.0]

Ball [colour=Black, radius=5.0]

===========================================================

2.

package BlueJ;

public class Message {

   private String sender;

   private String recipient;

   private String messageText;

   /**

   * @param sender

   * @param recipient

   */

   public Message(String sender, String recipient) {

       super();

       this.sender = sender;

       this.recipient = recipient;

   }

  

   /**

   * @param messageText the messageText to set

   */

   public void append(String messageText) {

       this.messageText = messageText;

   }

   /* toString()

   */

   @Override

   public String toString() {

       return "From:" + sender + "%nTo:" + recipient + "%nText:" + messageText + """;

   }

}

---------------------------------------------------------------------------------------

package BlueJ;

import java.util.HashMap;

import java.util.Scanner;

public class Mailbox {

   private HashMap<Integer, Message>storeMessage;

   private int messageCount;

   public Mailbox() {

       storeMessage= new HashMap<>();

       messageCount = 0;

   }

   //add message in mailbox

   public void addMessage(Message m){

       messageCount = messageCount+1;

       storeMessage.put(messageCount, m);

      

   }

   //get message from mailbox at specific index

   public Message getMessage(int i){

       return storeMessage.get(i);

   }

   //remove message from mailbox at specific index

   public void removeMessage(int i){

       storeMessage.remove(i);

   }

   public static void main(String[] args) {

       Mailbox mail = new Mailbox();

       Scanner sc = new Scanner(System.in);

       int n ;

       boolean quit = false;

       //run when not quit

       while(!quit){

           System.out.println("enter 1 for New Message 2 for remove Message 3 for Quit");

           try{

           n = sc.nextInt();

           sc.nextLine();

               switch (n) {

               //add message condition

               case 1:

                   System.out.println("enter sender name");

                   String sender = sc.nextLine();

                   System.out.println("enter reciever name");

                   String reciever = sc.nextLine();

                   Message msg = new Message(sender, reciever);

                   System.out.println("enter message text");

                   msg.append(sc.nextLine());

                   mail.addMessage(msg);

                   System.out.println("Your Message "+ msg.toString() +" is added.");

                   break;

                   //remove message condition

               case 2:

                   System.out.println("Please choose message index from list of messahges");

                   //print list of messages

                   for(int i = 1; i<=mail.messageCount;i++){

                       System.out.println(i +". "+ mail.getMessage(i));

                   }

                   mail.removeMessage(sc.nextInt());

                   System.out.println("Your Message is removed.New List are : ");

                   for(int i = 1; i<=mail.messageCount;i++){

                       System.out.println(i +". "+ mail.getMessage(i));

                   }

                   break;

               case 3:

                   quit = true;

                   System.out.println("You successfully quit from application");

                   break;

               default:

                   System.out.println("You enter an invalid option");

                   break;

               }

           }

           catch(Exception e){

               System.out.println("You enter an invalid option");

           }

       }

       sc.close();

   }

}

--------------------------------------------------

Sample Output:

enter 1 for New Message 2 for remove Message 3 for Quit

1

enter sender name

shubham

enter reciever name

annonyms

enter message text

if u have any prob i can help you.

Your Message From:shubham%nTo:annonyms%nText:if u have any prob i can help you." is added.

enter 1 for New Message 2 for remove Message 3 for Quit

2

Please choose message index from list of messahges

1. From:shubham%nTo:annonyms%nText:if u have any prob i can help you."

1

Your Message is removed.New List are :

1. null

enter 1 for New Message 2 for remove Message 3 for Quit

3

You successfully quit from application