Question

In: Computer Science

Java - Write an abstract class called Shape with a string data field called colour. Write...

Java - Write an abstract class called Shape with a string data field called colour. Write a getter and setter for colour. Write a constructor that takes colour as the only argument. Write an abstract method called getArea()

Solutions

Expert Solution

Here is the code for the problem:

//The abstract class called Shape
abstract class Shape {
   //The strin variable to set the color
   String color;

   Shape(String color){
       this.color = color;
   }

   //getter method for color
   public String getColor() {
       return color;
   }
   //setter method for color
   public void setColor(String color) {
       this.color = color;
   }

   abstract void getArea();
}
//Driver class that extends the above abstrcat class
public class AbstractClassExample extends Shape {
   //The test class constructor
   AbstractClassExample() {
       //calls the Super class constructor
       super("Blue");
   }
  
   void getArea()
   {
       System.out.println("The getArea() method");
   }
   public static void main(String[] args) {
       //Instantiating the abstract class using the extended class
       AbstractClassExample shape = new AbstractClassExample();
       //setting the color of the shape
       shape.setColor("Blue");
       //output the color set for the shape
       System.out.println("The color set is: " + shape.getColor());

       shape.getArea();
   }
}

SCREENSHOT:

OUTPUT:


Related Solutions

Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...
Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level,...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level, cottage, etc.) and size. Provide the following methods: A no-arg/default constructor. A constructor that accepts parameters. A constructor that accepts the type of the house and sets the size to 100. All other required methods. An abstract method for calculating heating cost. Come up with another abstract method of your own. Then write 2 subclasses, one for mobile house and one for cottage. Add...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions:...
In C++ Create an abstract class called Shape Shape should have the following pure virtual functions: getArea() setArea() printArea() Create classes to inherit from the base class Circle Square Rectangle Both implement the functions derived from the abstract base class AND must have private variables and functions unique to them like double Radius double length calculateArea() Use the spreadsheet info.txt read in information about the circle, rectangle, or square text file: circle   3.5   square   3   rectangle   38   36 circle   23  ...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
Using java: Write a class called StringAlternateSorting what it does: 1. ask user for one String...
Using java: Write a class called StringAlternateSorting what it does: 1. ask user for one String (String1) (must be sorted) 2. ask user for second String (String2) (must be sorted) 3. Then prints all the characters of two previous Strings, in such a way that they are sorted (see examples below). 4. consider the possibility that strings are of different sizes, one or both can be empty, etc. 5. strings can contain any kind of characters as long as they...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT