Question

JAVA INTERFACE 1.Suppose you want to design a class that is given numbers one at a...

JAVA INTERFACE

1.Suppose you want to design a class that is given numbers one at a time. The class computes the smallest, second smallest, and average of the numbers that have been seen so far. Create an interface for the class. Create a class that implements the interface.

2. Create an interface Measurable with methods getarea( )and getperimeter(). Implement it in two classes Circle, and Square . Both the classes should have Constructor for initializing the dimensions, and define the interface methods.

Create a class client which creates an object of Circle and Square and call their methods.

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

Please find the answer

create a package and create all class and Interface with the same name as given below inside the package and copy code as given .

If you have any query please let me know first i will give resolution

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

Answer 1- all Classes and Interface

Interface -->

ComputeNumber.java

public interface ComputeNumber {

void smallest(int[] arr);

void secondSmallest(int[] arr);

void average(int[] arr);

}

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

implementation class

ComputeNumberImpl.java

public class ComputeNumberImpl implements ComputeNumber {

/**

* this smallest(int[] arr) method is the implementation of ComputerNumber Interface

* here we calculate the smallest of give number of array

* in this method we pass the array as argument

*/

@Override

public void smallest(int[] arr) {

// first suppose the array at zero position store in a variable

// and compare the value loop through the each element of array

// if the smallest number is found at arr[i] position then it store in smallestNumber variable and print it

int smallestNumber = arr[0];

for (int i = 0; i < arr.length; i++) {

if (arr[i] < smallestNumber) {

smallestNumber = arr[i];

}

}

System.out.println("Smallest Number is = " + smallestNumber);

}

/**

* this second smallest(int[] arr) method is the implementation of ComputerNumber Interface

* here we calculate the second smallest number of the given array

* in this method we pass the array as argument

*/

@Override

public void secondSmallest(int[] arr) {

// here take a vaibale temp and it is to swap a element

// here two loop i and j

// first loop for array element at zero position and compare these value to next value from it

// if the element arr[i] found the first smallest and arr[j] found second smallest swap it and

int temp;

for (int i = 0; i < arr.length; i++) {

for (int j = i + 1; j < arr.length; j++) {

if (arr[i] > arr[j]) {

temp = arr[i];

arr[i] = arr[j];

arr[j] = temp;

}

}

}

// second smallest value is store at arr[1] means second position of array and print it

System.out.println("Second Smallest Number = " + arr[1]);

}

/**

* this average(int[] arr) method is the implementation of ComputerNumber Interface

* here we calculate the average number of the given array

* in this method we pass the array as argument

*/

@Override

public void average(int[] arr) {

double average,sum=0;

//calculate size of array

int size =arr.length;

for (int i = 0; i < arr.length; i++) {

sum=sum+arr[i];

}

// average is sum/size

average=sum/size;

// print the average value

System.out.println("Smallest Number is = " + average);

}

}

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

Main class to compute the smallest , second smallest and average of number

ComputeNumberMain.java

public class ComputeNumberMain {

public static void main(String[] args) {

//take a array of number and pass this array into a method

int arr[] = { 23, 6, 43, 8, 9, 10 };

// create the object of ComputeNumberImpl

ComputeNumberImpl obj = new ComputeNumberImpl();

// object call the smallest method to print the smallest number;

obj.smallest(arr);

// object call the secondSmallest method to print the second smallest number;

obj.secondSmallest(arr);

// object call the average method to print the average number;

obj.average(arr);

}

}

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

Output

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

Answer 2- Classes and Interface

Interface

Measurable.java

public interface Measurable {

void getarea();

void getperimeter();

}

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

Circle class and this circle class implements Mesurable Interface

Circle.java

public class Circle implements Measurable {

double radius;

// constructor create to pass the radius of each method

Circle(int radius)

{

this.radius=radius;

}

// method to print the area of circle

@Override

public void getarea() {

  

double area;

// area = PI*radius*radius here PI=3.14 but we use Math.PI of java

area=radius*radius*Math.PI;

// here we use printf to format value at two decimal place here .2f is use for this purpose

System.out.printf("Area of Circle = %.2f \n" , area);

}

// method to print the perimeter of circle

@Override

public void getperimeter() {

double perimeter;

// perimeter = 2*radius*PI here PI=3.14 but we use Math.PI of java

perimeter=2*radius*Math.PI;

// here we use printf to format value at two decimal place here .2f is use for this purpose

System.out.printf("Perimeter of Circle = %.2f",perimeter);

System.out.println("");

}  

}

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

Square class and this Square class implements Mesurable Interface

Square.java

public class Square implements Measurable {

//variable declare

double side;

//constructor

Square(double side) {

this.side = side;

}

@Override

public void getarea() {

// area of square side*side

double area;

area = side * side;

// here we use printf to format value at two decimal place here .2f is use for

// this purpose

System.out.printf("Area of Square = %.2f \n", area);

}

@Override

public void getperimeter() {

// perimeter of square is 4*side

double perimeter;

perimeter = 4*side;

// here we use printf to format value at two decimal place here .2f is use for

// this purpose

System.out.printf("Perimeter of Square = %.2f \n", perimeter);

}

}

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

Main class i.e Client

// client class

public class Client {

public static void main(String[] args) {

//value pass for radius of circle

// here object is create for Circle class

Circle circle=new Circle(4);

circle.getarea();

circle.getperimeter();

//for space

System.out.println(" ");

// here object is create for Square class

//and pass the value to constructor of rectangle

Square square=new Square(4.5);

square.getarea();

square.getperimeter();

}

}

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

output

If you have any query please let me know first i will give resolution

Add a comment
Know the answer?
Add Answer to:
JAVA INTERFACE 1.Suppose you want to design a class that is given numbers one at a...
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
  • SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The...

    SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: A private variable of type double named radius to represent the radius. Set to 1. Constructor Methods: Python : An overloaded constructor method to create a default circle. C# & Java: Default Constructor with no arguments. C# & Java: Constructor method that creates a circle with user-specified radius. Method getRadius() that returns the radius. Method setRadius()...

  • Design and implement class Circle to represent a circle object. The class defines the following attributes...

    Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: 1.      A private variable of type double named radius to represent the radius. Set to 1. 2.      Constructor Methods: •        Python : An overloaded constructor method to create a default circle. •        C# & Java: Default Constructor with no arguments. •        C# & Java: Constructor method that creates a circle with user-specified radius. 3.      Method getRadius() that returns the radius. 4.     ...

  • (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal a...

    (The interface class-like) Assume you have the Edible interface with its abstract method. Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make implement the Edible interface. howToEat() and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML diagram for the classes and the interface 2. Use Arraylist class to create an...

  • Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design...

    Exercise 8 (The interface class-like) Assume you have the Edible interface with its abstract method Design a class named Animal and its two subclasses named Mammal and Dairy. Make Sheep and Bear as subclasses of Mammal and make Chicken and Cow as subclasses of Dairy. The Sheep and Dairy classes implement the Edible interface. howToEat) and sound() are the main two methods for all edible classes while sound() is the main method for the non-edible classes. 1. Draw the UML...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public...

    Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String input =""; System.out.println("Welcome to the easy square program"); while(true) { System.out.println("Enter the length of the side of a square or enter QUIT to quit"); try { input = keyboard.nextLine(); if(input.equalsIgnoreCase("quit")) break; int length = Integer.parseInt(input); Square s = new Square(); s.setLength(length); s.draw(); System.out.println("The area is "+s.getArea()); System.out.println("The perimeter is "+s.getPerimeter()); } catch(DimensionException e)...

  • JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class...

    JAVA Problem:  Coffee    Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type, price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public void prepare (); } The prepare method will...

  • Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named...

    Problem: Coffee(Java) Design and implement a program that manages coffees. First, implement an abstract class named Coffee. The Coffee class has three member variables: coffee type(does not mention the data type of the variable coffeeType), price, and store name. The Coffee class has an abstract method name ingredient. Make sure to add all necessary setters and getters as well as other methods. The class Coffee implements an interface HowToMakeDrink. The interface has the following method: public interface HowToMakeDrink { public...

  • Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following...

    Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: · Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. · A no-arg constructor that creates a default rectangle. · A constructor that creates a...

  • Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An...

    Suppose we have the following Java Interface: public interface Measurable {    double getMeasure(); // An abstract method    static double average(Measurable[] objects) { // A static method    double sum = 0;    for (Measurable obj : objects) {    sum = sum + obj.getMeasure();    }    if (objects.length > 0) { return sum / objects.length; }    else { return 0; }    } } Write a class, called Person, that has two instance variables, name (as...

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