Hello my Chegg friends, can someone give me a little help with the below problem
ID: 3855643 • Letter: H
Question
Hello my Chegg friends, can someone give me a little help with the below problem, if there is one. The book for the class is Introduction to Java Programming, Comprehensive Version Y. Daniel Liang & Prentice Hall 11th Edition / 2018. We are currently working in Chapter 10. This chapter covers the following:
• Class Abstraction
• Encapsulation
• Class Relationships
• Object-Oriented Paradigm
• Primitive/Wrapper Class Conversions
• String Class
We have a subclass Circle() in our project. It is instantiated as an object, myCircle(). In myCircle, we have a get and set method to determine the area as such:
//Set the area
private void setArea(double r) {
area = r * r * Math.PI;
}
//Return the area
private double getArea(){
return area;
}
In the main class, we are calling the set and get methods as such:
//Create a Circle object
Circle myCircle = new Circle();
System.out.print("Enter a radius: ");
radius = sc.nextDouble();
myCircle.setArea(radius);
System.out.printf("The area of this circle is: %.
2f ",myCircle.getArea());
What is wrong with this code (if anything)? Will it work? If not, how could it be fixed?
***Note: Assume the scanner object has been created.
Explanation / Answer
Answer: I dont see any issue with this code. It will work for sure. Below code is correct,
Scanner sc = new Scanner(System.in);
//Create a Circle object
Circle myCircle = new Circle();
System.out.print("Enter a radius: ");
radius = sc.nextDouble();
myCircle.setArea(radius);
System.out.printf("The area of this circle is: %.
2f ",myCircle.getArea());
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.