Java Project Module 5 Abstract Classes and Interfaces Assign

Java Project

Module 5: Abstract Classes and Interfaces

Assignment 5: TestAll

The primary focus of chapter five is the use of interfaces. You may remember that we discussed interfaces in the chapter one assignment. In programming, an interface is a public avenue by which data can be accessed and/or manipulated. In essence, all Java classes contain an interface -- that is, they declare at least some public methods that can be called by other objects. If a class was designed in such a way that it provided no public interface, the class would be completely useless. For example, an object of a class cannot be instantiated without a public constructor; additionally, non-public instance variables defined within a class would have no purpose if no methods were furnished to manipulate or (at least) access those variables. Interfaces are essential; they provide the means by which objects may communicate with each other and perform tasks. An object that has no interface is deaf, dumb, and blind -- it cannot receive data, return data, or interact with other objects in any way. For an object to be useful, it must implement an interface -- that is, it must contain public methods that can be called by other objects. For this reason and others, the Java programming language provides the interface declaration.

Unlike older object-oriented programming languages, such as C++, Java provides an explicit way of creating an interface to a class -- theinterface declaration. In Java, an interface declaration is very similar to the declaration of a class, except that an interface declaration is completely abstract and public. Interfaces declare methods with specific names and arguments but do not define the bodies of those methods. Interfaces also do not declare instance variables or contain any kind of private declarations. A Java interface is like a silhouette -- an outline of a class that describes what the class can do but not how it works. Once an interface is declared, any number of classes can implement the interface in order to signal that they are capable of performing specific tasks.

Let\'s use an analogy to describe the relationship between classes and interfaces: Imagine that a programmer must design several classes for a video game. The game is a car racing game in which the user can choose from a variety of cars. When the game first begins, the user is presented with a list of cars, and each car has different specifications, such as make, model, year, engine size, color, etc. As the user progresses through each level of the game, he is awarded money which he can use to upgrade his car. The user may purchase parts, nitro boosters, custom paint jobs, decals, etc. How should the programmer handle these variations? The first solution that comes to mind is to design a class calledCar, which contains a number of instance variables to keep track of improvements made to a car. However, not all cars are the same. A 2010 Chevy Corvette Convertible shares very little similarity with a 1984 Dodge Station Wagon -- other than the fact that they both have four tires and a motor! Different types of cars maneuver, accelerate, and appear very differently. Some cars have more than two brake lights, some cars have hatchbacks, some cars have sun roofs, and some cars are convertibles. Obviously, a single class couldn\'t do it all -- at least not without being designed in a very confusing and incredibly dense manner. A better method of handling all of these variations would be to design an interface called Car, which declares only the methods that apply to all cars, such as getMake(), getModel(), getYear(),getEngineSize(), getColor(), etc. Afterward, specific classes such as Corvette and Wagon could be designed to implement theCar interface, and each separate class could be designed with very specific features in mind. For example, the Corvette class could be designed with convertibles in mind, whereas the Wagon class does not need to be concerned with this particular feature. After the programmer has declared the Car interface, along with a few specific types of cars, he may begin work on creating the list of cars presented to the user at the start of the game. Rather than depend on a single class that may need to be completely overhauled to incorporate new features as new cars are added to the game, the programmer is free to design much of the game without having to worry about his interface changing. Thus, the programmer has avoided painting himself into a corner. Once the game is finished, he may even decide to create an improved version of the game, and, if he designed his interface well, he will still be able to use it in the newer version.

In this assignment students will create several classes that implement an interface. Before attempting the assignment, students should carefully read and study Chapter 2 in the textbook.

Goals:

Through the completion of this assignment students should familiarize themselves with the creation of an interface. Students should understand the purpose of organizing methods into an interface so that multiple classes can share the same functionality. Students should also note that implementation of an interface provides a simple way for classes to be organized within a single category without the need for complex inheritance, which can be troublesome and easily leads to code rot -- software that loses its reusability over time.

Files to be Submitted:

In this assignment students should create five Java classes called Point, Circle, Square, Rectangle, and TestAll, as well as a Java interface called FigureGeometry. The TestAll class should implement a main method which tests all of the other files created in the assignment. After the assignment has been completed, all six files should be submitted for grading into the Dropbox for Assignment 2, which can be found in the Dropbox menu on the course website. Students should note that only the *.java file for each class/interface needs to be submitted; No *.class files need to be submitted into the Dropbox.

Please click on each of the following links to view specific requirements for the files that must be created and submitted:

Requirements: Point.java

Requirements: FigureGeometry.java

Requirements: Circle.java

Requirements: Square.java

Requirements: Rectangle.java

Requirements: TestAll.java

Tips:

The following code example displays a basic implementation of an interface:


//DummyInterface.java:

public interface DummyInterface
{

    final int SOME_CONSTANT = 1234;

    void someMethod();

}


//DummyClass.java:

public class DummyClass implements DummyInterface
{

    void someMethod()
    {
        System.out.println( SOME_CONSTANT );
    }

}

Solution

Please let me know if you need more information:-
---------------------------------------------------------------------------

FigureGeometry.java

---------------------------------------------------
public interface FigureGeometry {
   final int CONSTANT = 1234;
   public void Shape(String color);
   public String getColor();
}


---------------------------

Circle.java

------------------------------------


public class Circle implements FigureGeometry {

   private double radius;

   public Circle(String color, double radius) {
       this.radius = radius;
   }

   public double area() {
       return ((22 / 7) * radius * radius);
   }

   public String toString() {
       return this.getColor() + \" circle with radius of \" + radius
               + \" area of \" + area();
   }

   @Override
   public void Shape(String color) {
       // TODO Auto-generated method stub
      
   }

   @Override
   public String getColor() {
       // TODO Auto-generated method stub
       return null;
   }

}


-----------------------------------------------------

Square.java
---------------------------------------------------


public class Square implements FigureGeometry{
   private double side_length;
   public Square(String color, double side_length) {
       this.side_length = side_length;
   }
  
   public double area() {
       return (side_length * side_length);
   }
   public String toString() {
       return this.getColor() + \" circle with side length of \" + side_length
               + \" area of \" + area();
   }

   @Override
   public void Shape(String color) {
       // TODO Auto-generated method stub
      
   }

   @Override
   public String getColor() {
       // TODO Auto-generated method stub
       return null;
   }
}

------------------------------------------------------------------

Rectangle.java
----------------------------------------------------------------------


public class Rectangle implements FigureGeometry{
   double width;
   double length;
   public Rectangle(String color, double width,double length) {
       this.width = width;
       this.length = length;
   }
   public double area() {
       return (width * length);
   }
   public String toString() {
       return this.getColor() + \" circle with length of \"+this.length+\" and width of \"+this.width
               + \" area of \" + area();
   }
   @Override
   public void Shape(String color) {
       // TODO Auto-generated method stub
      
   }
   @Override
   public String getColor() {
       // TODO Auto-generated method stub
       return null;
   }
}


-------------------------------------------------------------------------------

Point.java

-----------------------------------------------------------------------


public class Point implements FigureGeometry{

   @Override
   public void Shape(String color) {
       // TODO Auto-generated method stub
      
   }

   @Override
   public String getColor() {
       // TODO Auto-generated method stub
       return null;
   }

}

----------------------------------------------------------------

TestAll.java

-----------------------------------------------------------------


public class TestAll {

}

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

Thanks

Java Project Module 5: Abstract Classes and Interfaces Assignment 5: TestAll The primary focus of chapter five is the use of interfaces. You may remember that w
Java Project Module 5: Abstract Classes and Interfaces Assignment 5: TestAll The primary focus of chapter five is the use of interfaces. You may remember that w
Java Project Module 5: Abstract Classes and Interfaces Assignment 5: TestAll The primary focus of chapter five is the use of interfaces. You may remember that w
Java Project Module 5: Abstract Classes and Interfaces Assignment 5: TestAll The primary focus of chapter five is the use of interfaces. You may remember that w
Java Project Module 5: Abstract Classes and Interfaces Assignment 5: TestAll The primary focus of chapter five is the use of interfaces. You may remember that w

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site