CSC122Module6Fall2021Student/CSC122Module6Lab2021.docx Lab 6: Writing Classes 1. Sphere Design and implement a class called Sphere that contains instance data for: · The sphere’s diameter. · The...

1 answer below »


A NetBeans project named "Lab6” containing



Your source code for:




  • Sphere.java


  • SphereDemo.java



Run SphereDemo and paste your test run output to the bottom of your SphereDemo file and then comment out that output.



Tip: In NetBeans you can select your project and select File...Export Project to zip, and then submit the zip file to me.




CSC122Module6Fall2021Student/CSC122Module6Lab2021.docx Lab 6: Writing Classes 1. Sphere Design and implement a class called Sphere that contains instance data for: · The sphere’s diameter. · The sphere’s id number The class must also keep count of the number of spheres created. Define the Sphere constructor to accept and initialize the diameter, keep count of the number Spheres created, and automatically set an id number (based on the count) for the Sphere. The class will include getters and setters for the diameter, and a getter for the id number of the Sphere, and a getter for the count of the number of Spheres created. Include code in your set method for the diameter of the Sphere that maintains consistent state for a Sphere (value for the diameter of a Sphere must be greater than 0). Once you have created your setter for diameter, you should call that setter from within the constructor to insure that the diameter of your new sphere has consistent state. Let’s assume that if the user attempts to create an invalid Sphere, that the constructor will then set the diameter to 1. Include methods that calculate and return the volume and surface area of the Sphere (see programming project 3.5 in chapter 3 for the formulas). Include a toString method that returns a one-line String representation of the sphere. I have created a driver/client class called SphereDemo whose main method instantiates 4 Sphere objects and updates the second Sphere object. Make no changes to SphereDemo. I have included SphereDemo.java in your Module 6 download. Class Sphere must be written so that it works seamlessly with SphereDemo. All of the tools that you need to solve this problem are covered in the Module 5 and 6 videos, slides and textbook examples, specifically pages 170 – 203. Concepts that you must address in this problem include: visibility, instance variables, constructors, a toString method, accessors and mutators, methods and their return types, and static class members. Test your program using the data listed below. Test Run Enter the diameter for the first sphere: 4.75 Sphere 1 diameter: 4.75 volume: 56.115 area: 70.882 Enter the diameter for the second sphere: 20.00 Sphere 2 diameter: 20.0 volume: 4188.79 area: 1256.637 Enter the diameter for the third sphere: 11.946 Sphere 3 diameter: 11.946 volume: 892.619 area: 448.327 Change the diameter for the second sphere to: 18.4 Sphere 2 diameter: 18.4 volume: 3261.761 area: 1063.618 Enter the diameter for an invalid fourth sphere: -3.5 Sphere 4 diameter: 1.0 volume: 0.524 area: 3.142 Client Code import java.util.Scanner; public class SphereDemo { //----------------------------------------------------------------- // Creates and exercises some Sphere objects. //----------------------------------------------------------------- public static void main(String[] args) { double diameter; Scanner scan = new Scanner(System.in); System.out.println("Enter the diameter for the first sphere: "); diameter = scan.nextDouble(); Sphere s1 = new Sphere(diameter); System.out.println(s1 + "\n"); System.out.println("Enter the diameter for the second sphere: "); diameter = scan.nextDouble(); Sphere s2 = new Sphere(diameter); System.out.println(s2 + "\n"); System.out.println("Enter the diameter for the third sphere: "); diameter = scan.nextDouble(); Sphere s3 = new Sphere(diameter); System.out.println(s3 + "\n") ; System.out.println("Change the diameter for the second sphere to: "); diameter = scan.nextDouble(); s2.setDiameter(diameter); System.out.println(s2 + "\n"); System.out.println("Enter the diameter for an invalid fourth sphere: "); diameter = scan.nextDouble(); Sphere s4 = new Sphere(diameter); System.out.println(s4 + "\n"); } } CSC122Module6Fall2021Student/SphereDemo.java CSC122Module6Fall2021Student/SphereDemo.java //******************************************************************** //  MultiSphere.java       Java Foundations // //  Solution to Programming Project 5.4 //******************************************************************** import java.util.Scanner; public class SphereDemo {     //-----------------------------------------------------------------     //  Creates and exercises some Sphere objects.     //-----------------------------------------------------------------     public static void main(String[] args)     {             double diameter;                         Scanner scan = new Scanner(System.in);             System.out.println("Enter the diameter for the first sphere: ");             diameter = scan.nextDouble();             Sphere s1 = new Sphere(diameter);             System.out.println(s1 + "\n");                          System.out.println("Enter the diameter for the second sphere: ");             diameter = scan.nextDouble();             Sphere s2 = new Sphere(diameter);             System.out.println(s2 + "\n");                          System.out.println("Enter the diameter for the third sphere: ");             diameter = scan.nextDouble();             Sphere s3 = new Sphere(diameter);             System.out.println(s3 + "\n") ;                          System.out.println("Change the diameter for the second sphere to: ");             diameter = scan.nextDouble();             s2.setDiameter(diameter);             System.out.println(s2 + "\n");                          System.out.println("Enter the diameter for an invalid fourth sphere: ");             diameter = scan.nextDouble();             Sphere s4 = new Sphere(diameter);             System.out.println(s4 + "\n");                  }          }
Answered 2 days AfterOct 22, 2021

Answer To: CSC122Module6Fall2021Student/CSC122Module6Lab2021.docx Lab 6: Writing Classes 1. Sphere Design and...

Saima answered on Oct 24 2021
112 Votes
Sphere/MultiSphere/.classpath

    
        
            
        
    
    
    
Sphere/MultiSphere/.project

     Mu
ltiSphere
    
    
    
    
        
             org.eclipse.jdt.core.javabuilder
            
            
        
    
    
        ...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here