Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Java Alter the circle class provided here to include a compareTo method. With this class we will compare circle objects according to their radius.

In Java

Alter the circle class provided here to include a compareTo method. With this class we will compare circle objects according to their radius. DO NOT change the toString method (or your output will be wrong). Input is from the keyboard and consists first of an integer to tell you how many doubles will follow. Each double is the radius of a circle. Add code to the main method as specified.

import java.util.ArrayList; import java.util.Collections; import java.util.Scanner;

public class Lab7Num1

{ public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); ArrayListmyC = new ArrayList(); //input the number of circles int howMany = keyboard.nextInt(); //loop howMany times, //each time input a double, create a circle, add it to the ArrayList //sort the ArrayList //output the ArrayList } }

public class Circle implements Comparable { //attribute private double radius; //constructors public Circle() {radius=0.0; } public Circle(double r) {radius=r; } //accessors public double getRadius() { return radius; } //mutators public void setRadius(double r) { radius = r; } //methods public double circumference() { return 2*Math.PI*radius; } public double area() { return Math.PI*radius* radius; } public String toString() { return "Circle of radius " + radius; } public int compareTo(Circle c) { //write your code here } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions