create a new program containing: A class named Point, with two fields: x and y and a constructor to set the values (see https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/java/javaOO/examples/Point.java) Add a new field that will keep a name for the point. It is empty string by default. Create a new constructor that will set the name to a predefined value. Add a print method that will print the coordinates in the format: NAME(X,Y). A class name Rectangle, with a point of origin, height and width, four constructors and two methods (see https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/java/javaOO/examples/Rectangle.java) Add a new field that will keep a name for the rectangle. Create a constructor that will set the name, origin point and dimensions. Add a print method that will print the coordinates in the format: NAME(x, y, w, h)
Code:
import java.util.*;
//Bicycle interface
interface Bicycle
{
abstract void changeCadence(int newValue); //will change value of candence to new value
abstract void changeGear (int newValue); //changes gear of car
abstract void speedUp(int increment); //increments speed of car by adding new Value to existing speed
abstract void applyBrakes(int decrement);
}
//ACMEBicycle class definition
class ACMEBicycle implements Bicycle
{
int cadence = 0; Â Â
int speed = 0; Â Â
int gear = 1;
//methods of interface
public void changeCadence(int newValue)
{
this.cadence=newValue;
}
public void changeGear (int newValue)
{
this.gear=newValue;
}
public void speedUp(int increment)
{
this.speed+=increment;
}
public void applyBrakes(int decrement)
{
this.speed-=decrement;
}
//display method
void display()
{
System.out.println("Cadence: "+this.cadence);
System.out.println("Gear: "+this.gear);
System.out.println("Speed: "+this.speed);
}
}
//KEYOBicycle class definition
class KEYOBicycle implements Bicycle
{
int cadence = 0;
int speed = 0;
int gear = 1;
//methods of interface
public void changeCadence(int newValue)
{
this.cadence=newValue;
}
public void changeGear (int newValue)
{
this.gear=newValue;
}
public void speedUp(int increment)
{
this.speed+=increment;
}
public void applyBrakes(int decrement)
{
this.speed-=decrement;
}
//display method
void display()
{
System.out.println("Cadence: "+this.cadence);
System.out.println("Gear: "+this.gear);
System.out.println("Speed: "+this.speed);
}
}
public class Main
{
public static void main(String[] args) {
System.out.println("Enter 1 for ACMEBicycle or 2 for KEYOBicycle");
Scanner sc= new Scanner(System.in);
//prompts user to choose either of the bicycles
int a=sc.nextInt();
//creating objects
ACMEBicycle obj1= new ACMEBicycle();
KEYOBicycle obj2= new KEYOBicycle();
if (a==1)
{
obj1.changeCadence(5);
obj1.changeGear(3);
obj1.speedUp(40);
obj1.applyBrakes(20);
obj1.display();
}
else if (a==2)
{
obj2.changeCadence(5);
obj2.changeGear(3);
obj2.speedUp(40);
obj2.applyBrakes(20);
obj2.display();
}
else
{
System.out.println("Wrong Choice!");
}
}
}
Using this links:
Classes and Objects https://docs.oracle.com/javase/tutorial/java/javaOO/index.html
- Objects https://docs.oracle.com/javase/tutorial/java/javaOO/objects.html
- Creating Objects https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
- Using Objects https://docs.oracle.com/javase/tutorial/java/javaOO/usingobject.html Instructions:
-
Continue the above code(program):
Also:
- create a new program containing:
- A class named Point, with two fields: x and y and a constructor to set the values (see https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/java/javaOO/examples/Point.java)
- Add a new field that will keep a name for the point. It is empty string by default. Create a new constructor that will set the name to a predefined value.
- Add a print method that will print the coordinates in the format: NAME(X,Y).
- A class name Rectangle, with a point of origin, height and width, four constructors and two methods (see https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/java/javaOO/examples/Rectangle.java)
- Add a new field that will keep a name for the rectangle.
- Create a constructor that will set the name, origin point and dimensions.
- Add a print method that will print the coordinates in the format: NAME(x, y, w, h)
- A class named Point, with two fields: x and y and a constructor to set the values (see https://docs.oracle.com/javase/tutorial/displayCode.html?code=https://docs.oracle.com/javase/tutorial/java/javaOO/examples/Point.java)
- Imagine a robot head (square) with a rectangle mouth and nose and point eyes. Create the image in a new main program (class), giving suggestive names for points and rectangles.
- Print the information for all the figures.
- Check which eye has the biggest x coordinate and print the result.
Step by step
Solved in 2 steps with 1 images