Question

I am taking a computer science course and my professor teaches much too fast. I am...

I am taking a computer science course and my professor teaches much too fast. I am feeling so overwhelmed and lost. Very worried as well, as I am trying my very hardest in this class but just cant seem to grasp it. I have just been given this assignment.... Can someone explain what I am supposed to do? Is there one class? two? are there supposed to be multiple files? :( thanks!


  • Write a Java class called a Circle that represents a circle. This circle has a field radius of type double.

  • (a) Import this class to a driver class circleDriver, and create an object of circle type. (b) What is its radius going to be? (c) Why?

  • Write a constructor that instantiates a circle with radius 3.5

  • Write a constructor that instantiates a circle with given radius

  • Test these constructors in circleDriver by initializing the constructors (3) as C1 and (4) as C2, and then printing C1.radius and C2.radius.

  • (a) What error does your program throw if you try to give your constructor a return type? (b) What does it mean? (one line)

  • (a) Write assessor (getter) for the field radius for this circles and test it in circleDriver: but before doing so, answer the following questions: (b) What should its return type be? (c) Why? (d) Why are you not passing any parameters to it?

  • (a)Change the fields of this circle in such a way that they are encapsulated, using private access modifier. (b)Why is it a good idea to encapsulate the fields? (one line) (c) what errors would printing C1.radius throw? (d) why?

  • (a)Write a class method called ‘Print’ to print the radius of Circle using your getter. (b)Test by printing C1’s radius.

  • (a) Write a setter to modify the radius value. But before you do that, answer the following questions: (b) what should its return type be? (c) Why? (d) why aren’t we passing any parameters to it?

  • Change the radius value of C1 to be 15 and print it with method Print.

  • (a) Write a class method that calculates the area of this circle. But before you do that, answer the following questions: (b) what should its return type be? (c) Why? (d) why aren’t we passing any parameters to it?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(a)Change the fields of this circle in such a way that they are encapsulated, using private access modifier.

private double radius;

(b)Why is it a good idea to encapsulate the fields? (one line)

ANS : BECAUSE IT PROTECTS CHANGING VARIABLE VALUE FROM OTHER CLASS and this method is called as encapsulation

(a) What error does your program throw if you try to give your constructor a return type? (b) What does it mean? (one line)

ANS : Constructor are special function without return type and should have same name as class name .If you add return type in constructer you will get error

Void methods cannot return a value

CircleDriver.java

//This is driver class,driver class is main class where main function is placed and called initially
public class CircleDriver {
   public static void main(String[] args) {
      //Test these constructors in circleDriver by initializing the constructors (3) as C1 and (4) as C2, and then printing C1.radius and C2.radius.
           Circle C1=new Circle();//create circle object
           //What is its radius going to be?
           //it will be 0.0,because default value of double variable is 0.0

           System.out.println(C1.radius);//print radius
           //Write a constructor that instantiates a circle with radius 3.5
     
    Circle C2=new Circle(3.5);//create circle object
           System.out.println(C2.radius);//print radius
           //(b)Test by printing C1’s radius.
           //this will print radius as 0.0 as there is memory associated to C1 object with radius as 0.0

           C1.print();
          //Change the radius value of C1 to be 15 and print it with method Print.
     
    C1.setRadius(15);
           C1.print();//will print 15.0
           System.out.println(C1.area());//will print 706.5
   }
}

Circle.java

public class Circle {
   double radius;
//parameterized constructer to initialize radius of users choice
  
   public Circle(double radius) {

       this.radius = radius;
   }
//default constructor
   public Circle() {
   }
   //(a) Write assessor (getter) for the field radius for this circles and test it in circleDriver:
   //but before doing so, answer the following questions:
   //(b) What should its return type
   //ITS RETURN TYPE WILL BE DOUBLE AS WE ARE ACCESSING DOUBLE VARIABLE
  
   //(d) Why are you not passing any parameters to it?
   //BECAUSE WE ARE ACCESSING IT'S VALUE OT INITIALIZING IT

   public double getRadius() {
       return radius;
   }

//(a) Write a setter to modify the radius value.
   //But before you do that, answer the following questions:
   //(b) what should its return type be?
  
   //IT's return type will be void as we are using it to just initializing value and not accessing it
  
   //(d) why aren’t we passing any parameters to it?
  
   //WE ARE PASSING PARAMETER TO IT OF DOUBLE TYPE

   public void setRadius(double radius) {
      
       this.radius = radius;
   }
   //(a)Write a class method called ‘Print’ to print the radius of Circle using your getter.
   public void print() {
       System.out.println(getRadius());
   }
   //a) Write a class method that calculates the area of this circle
   //(b) what should its return type be?
   //It should return double ,because we expect result in double from double value
   //(d) why aren’t we passing any parameters to it?
   //Because we want to only access data not to initialize any data

   public double area() {
       //circle area=3.14 * radius * radius;
       double Area=3.14*getRadius()*getRadius();
       return Area;
   }
}

Try executing this program and you will get your query solved :)

Add a comment
Know the answer?
Add Answer to:
I am taking a computer science course and my professor teaches much too fast. I am...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • [Continued] Your Course class should have: 1)a getter for the course 'code', so that it is...

    [Continued] Your Course class should have: 1)a getter for the course 'code', so that it is read-only; 2)a getter and setter for the lecturer attribute, so that it is readable and writable; 3)a constructor that creates the associations shown in the UML diagram; 4)an enrolStudent method that adds a given Student object into the 'enrolled' set; 5)a withdrawStudent method that removes a given Student object from the 'enrolled' set and returns true if the student was successfully found and removed,...

  • JAVA programing Question 1 (Date Class):                                   &nbsp

    JAVA programing Question 1 (Date Class):                                                                                                     5 Points Create a class Date with the day, month and year fields (attributes). Provide constructors that: A constructor that takes three input arguments to initialize the day, month and year fields. Note 1: Make sure that the initialization values for the day, month and year fields are valid where the day must be between 1 and 31 inclusive, the month between 1 and 12 inclusive and the year a positive number. Note 2:...

  • C++ Could you check my code, it work but professor said that there are some mistakes(check...

    C++ Could you check my code, it work but professor said that there are some mistakes(check virtual functions, prin() and others, according assignment) . Assignment: My code: #include<iostream> #include<string> using namespace std; class BasicShape { //Abstract base class protected: double area; private:    string name; public: BasicShape(double a, string n) { area=a; name=n; } void virtual calcArea()=0;//Pure Virtual Function virtual void print() { cout<<"Area of "<<getName()<<" is: "<<area<<"\n"; } string getName(){    return name; } }; class Circle : public...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • I am writing python code. I submitted an assignment but the professor said it was a...

    I am writing python code. I submitted an assignment but the professor said it was a modularization. Can you help me see what part of the code is modularization? Pseudocode: 1. Decalre MainMethod () function: # A. Parameters: Three numbers # B. Put the three numbers in reverse # 2. Main Program # A. Initialize Varibles # B. Accepts three values from the user # C. Invoke MainMethod () function, passing the three numbers as arguments in reverse # D....

  • Could someone help me out. I am not sure what I should be doing. Seeing it...

    Could someone help me out. I am not sure what I should be doing. Seeing it worked out will allow me to understand what I should be doing and then I can complete it on my own. Usando 2. Complete the Dog Class: a. Using the UML Class diagram to the right declare the instance variables. A text version is available: UML Class Diagram Text Version b. Create a constructor that incorporates the type, breed, and name variables (do not...

  • Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggli...

    Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggling with a part of a question assigned by the professor. He gave us this code to fill in: import java.util.Arrays; import java.util.Random; public class RobotGo {     public static Random r = new Random(58);     public static void main(String[] args) {         char[][] currentBoard = createRandomObstacle(createBoard(10, 20), 100);         displayBoard(startNavigation(currentBoard))     }     public static int[] getRandomCoordinate(int maxY, int maxX) {         int[] coor = new...

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • My professor provided a very vague description of what he wants us to do with the...

    My professor provided a very vague description of what he wants us to do with the example code from the book. I have provided the picture of his description and the code from the book we need to alter. I'm already struggling with this course and I'll probably have to post another question on a different problem I'm stuck on. If you can help it would be highly appreciated. Description Change code for double, string, C-string with appropriate convert constructors...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT