Write a java program that creates two interfaces named test1 and test2. In interface test1 the member function is square. In interface test2 the member function is cube. Implement these two interfaces in "Arithmetic" class. Create one new class called ToTestInt in this class use the object of arithmetic class.
Java Code:
interface test1 //interface test1
{
abstract int square(int x);
}
interface test2 //interface test2
{
abstract int cube(int x);
}
class Arithmetic implements test1,test2{ //class Arithmetic
public int square(int x) //implementing square method
{
return x*x;
}
public int cube(int x) //implementing cube method
{
return x*x*x;
}
}
public class ToTestInt
{
public static void main(String[] args) {
Arithmetic a=new Arithmetic(); //creating an object of
Arithmetic class
int square=a.square(9); //using object of Arithmetic
class to call square method
int cube=a.cube(9); //using object of Arithmetic class
to call cube method
System.out.println(square);
//printing the square result
System.out.println(cube);
//printing the cube result
}
}
Output:
Note:
Please save the above program in a file name "ToTestInt.java" and then compile and execute it.
Please comment if you face any difficulty.
Write a java program that creates two interfaces named test1 and test2. In interface test1 the...
Java program. Code the following interfaces, implement them, and use the interface as a parameter, respectively: a. An interface called Printable with a void print() method. b. An interface called EmployeeType with FACULTY and CLASSIFIED integer data. The value of FACULTY is 1 and CLASSIFIED is 2. a. A class called Employee to implement the two interfaces. Its constructor will initialize three instance data as name, employeeType, and salary. Implementation of method print() will display name, employeeType, and salary; salary...
(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...
You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...
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...
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...
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...
Please teach me how to use function interface by java ● Write a functional interface StringOperator with the method performOperation that takes two references to String objects as parameters. ● In a driver program, use lambda expressions to create two different objects that implement the StringOperator interface. ○ One of them concatenates the two Strings, returning the new String ○ One of them sets the first String to all uppercase and the second String to all lowercase and then concatenates...
Java Code the following interfaces - An interface called Accountable with void withdraw(double) and double getBalance() methods, and an interface called AccountReceivable with a void deposit(double) method. Save these two interfaces in separate Java files. Then code a class, BusinessAccount, that implements the two interfaces defined above. Define and code the necessary instance variables and constructors. Override toString() so it will return the amount of a business account. Apply the particular operations, such as withdraw and deposit, required in implementing...
What all is true about Interfaces in Java? An interface contains function definitions (T/F) An interface contains function prototypes (T/F) A class extends an interface (T/F) A class implements an interface (T/F) An object reference can be of type interface (T/F) It is possible to make an object that's purely an interface type (T/F)
Write a Java interface called Salience that includes two methods: setSalience and getSalience. The interface should define a way to establish numeric salience among a set of objects. Design and implement a class called Task that represents a task (such as on a to-do list) that implements the Salience interface. Create a driver class to exercise some Task objects.