My assignment: Write a program that extends the example that we have done in class. Create a class called Trapezium, which is determined by two parallel sides “a” and “b” and the height “h” between them.The area of the trapezium can be calculated as Area = (a+b)h/2

The perimeter = a + b + 2 sqrt[{h2 + (b-a)2/4}]

Add this class to your project and add a couple of them to your object array. Print the array of objects. Sort the array and print it again.

**My code so far:

import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.util.Set;
import java.util.Map;

/**
 * Write a description of class trap here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public abstract class Trapezium extends GeometricObject { 
    private double width;
    private double height;

    public Trapezium () {

    }

    private int base1, base2, side;


    public Trapezium (int b1, int b2, int s)
    {
        base1 = b1;
        base2 = b2;
        side = s;
    }

    public int getBase1()
    {
        return base1;
    }

    public int getBase2()
    {
        return base2;
    }

    public int area()
    {
        int area = (((base1 + base2)*side)/2);
        return (int) area;
    }

    public int perimeter()
    {
       int perimeter=((base1 + base2 + 2)) * (sqrt((side*side)+(base2 - base1 )(base2-base1))/4);
        return perimeter;
    }

    public String toString()
    {
        return("b1=" + base1+ ", b2=" +base2+ ", side=" + side);
    }



}

****(NOT COMPILING) **

Help I am beyond lost!

you forgot to post the error messages
So with just this code here's my comments
line 50: If this is an issue, I don't see a sqrt() method perhaps you want to use the Math.sqrt() method or is there one defined in the GeometricObject class?

Next (which is probably the main problem), your missing an operator between these parentheses (base2 - base1 )(base2-base1) on the same line 50.

In addition to what's been said, my opinion is that you should prefer to not have unused import statements in your code. (Not that it will differ in terms of performance though).
Also my preference is that you specify exactly which class you want to use instead of using the asterisk wildcard.
The reason for this is that by importing per class you don't pollute the namespace you are using with names that you don't use, plus it is easy to see which classes are actually used by the code that follows.

Also, because different packages can contain classes with the same names, by importing the whole package you can run into a compiler error if you try to use a class which exists in both packages by not using its fully qualified class name.

A good example of this can be found in the Java API: java.util.List and java.awt.List.

Suppose you have code written like:

import java.util.*; // this package contains a class List
import java.awt.*;  // this package contains a class List too

// ...

// We want to use java.util.List here
List<Component> names; // will result in a compile-time error [1]

[1]

error: reference to List is ambiguous, both class java.awt.List
in java.awt and interface java.util.List in java.util match
        List d;
        ^
1 error

Rather do something like:

import java.util.List;     // import by specifying fully qualified classname
import java.awt.Component; // we also need the Component class from the java.awt package

// ...

// We want to use java.util.List here, is okay, there's no ambiguity now
List<Component> components;

After all it is an annoyance that you'd have to use the fully qualified classname to resolve such an ambiguity in the case that you only want to use one List class, but have imported both by specifying the wildcard.

To sum up:

Prefer not using wildcards and importing by specifying the fully qualified classname.
While the wildcard approach is shorter, that seems to be the only "advantage" I can think of.
In fact you could even turn that advantage into a disadvantage because it is not clear which classes from the package you're actually using [2].

On the other hand if you specify the fully qualified classname, you get the following advantages:

  • At the top of the source file, the import statements give an exact overview of the classes that are used.
  • If you are still learning Java it is good to type them out every time because it helps you in getting familiar with which classes can be found where.
  • You can avoid some ambiguity compiler errors.

[2] At least, not without scanning all the source code that follows, which could be thousands of lines.

Minor correction on my previous post:

"Prefer not using wildcards and importing by specifying the fully qualified classname."

should be:

"Prefer not using wildcards and - instead - import by specifying the fully qualified classname."

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.