Design A Class To Represent A Rectangle

Jan 8, 2015

I was told to design a class named Rectangle to represent a rectangle.The class contains:

■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height.
■ A no-arg constructor that creates a default rectangle.
■ A constructor that creates a rectangle with the specified width and height.
■ A method named getArea() that returns the area of this rectangle.
■ A method named getPerimeter() that returns the perimeter.

Draw the UML diagram for the class and then implement the class. Write a test program that creates two Rectangle objects one with width 4 and height 40 and the other with width 3.5 and height 35.9. Display the width, height, area,and perimeter of each rectangle in this order.Here is my code for the Rectangle Class:

class Rectangle
{
double width;
double height;
public Rectangle() {
width = 1;
height = 1;

[code]....

The error that I am given when I compile the driver is as follows:constructor Rectangle in class Rectangle cannot be applied to given types; required: no arguments; found:int,int; reason: actual and formal argument lists differ in length.

View Replies


ADVERTISEMENT

Declare A Class That Can Be Used To Represent A Quadrilateral

Jul 16, 2014

2. Declare a class called Quadrilateral that can be used to represent a quadrilateral. What instance variables are required? This class should include the following methods:

• Accessor and mutator methods. Notice that negative and zero lengths should not be accepted.
• A method called isParallelogram that returns a Boolean value indicating if the quadrilateral is a parallelogram.
• A method called isRectangle that indicates if the quadrilateral is a rectangle. This method should invoke the method isParallelogram and return a Boolean value.
• A method called isSquare that returns the Boolean value “true” if the quadrilateral is a square. This method should invoke the method isRectangle and return a Boolean value. URL...

import java.awt.Point;
public class Quadrilateral{
private Point p1, p2, p3, p4;
public Quadrilateral (Point p1, Point p2, Point p3, Point p4) {

this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.p4 = p4;
}
}

that is all i got, my professor has never mentioned on anything about quadrilateral,and i have 0 knowledge about this.

View Replies View Related

Create A Class Called Dice To Represent SINGLE Cube

Apr 15, 2014

I need to work on this "Dice" program. I've done it twice already. I've also been pouring over examples on here and elsewhere online, but none of them exactly match what I'm doing, as they all say "Pair of Dice".Mine says: "Create a class called Dice to represent a SINGLE cube". It should have a method called roll() that randomly selects a number from 1-6 for the value of the dice."

It has to use java.util.random, not math.java, and it has to have numberShowing:int, roll():int, and main() all in it.The last part reads "Create a test main method for the Dice class that creates a dice and rolls it many times. Can you keep track of how many times a number comes up? Describe how or implement it in the program." I have started at this computer for hours, and read as much info as I can.

View Replies View Related

Rectangle Class Include Try-catch And Exception Handling

Oct 18, 2014

The requirement is to write a rectangle class and a test class, which include try-catch blocks and exception handling. Exceptions, involving try, catch, throw, throws, and finally commands,how to write a code about basic things, but in the test class, it gives me specific width and height so that i dont konw how to write a try-catch blocks an exception handling in this test class.There is my two classes, they are separated.

public class Rectangle {
double width ;
double height ;
Rectangle(){
width = 1;
height = 1;

[code]....

View Replies View Related

How To Write Instance Method For Rectangle Class Called Contains

May 29, 2014

Write an instance method, contains, that has one explicit parameter of type Rectangle. The method should return true if every point of the rectangle determined by the explicit parameter is on or within the rectangle determined by the implicit parameter. It should return false otherwise.

This is what i did so far?

public boolean contains(Rectangle other) {
Rectangle intersect = Rectangle.intersection(this, other);
if ((intersect.left == this.left) && (intersect.bottom == this.bottom) && (intersect.width == this.width)
&& (intersect.height == this.height)) {
return true;
} else {
return false;
}
}

View Replies View Related

Design Class - How To Organize Classes For UNO Card Game

Mar 20, 2014

I'm currently taking a computer program design class which has done a lot for my understanding of how to organize classes, but isn't giving me challenging enough assignments and I don't believe it's going to be covering interfaces and abstract classes, which is a shame. So I've been digging into these topics myself and decided to work on my own program (an Uno game program) that would utilize everything we've been learning and give me some practice with GUIs.

My current plan:

Have an abstract UnoCard class that determines the basic properties/methods common to all cards. Create a class for each card type extending from UnoCard, which would be - the generic card (number and color), action cards (skip, reverse, draw two), and special cards (wild, wild draw four, and blank).

Two enums, one for color, one for rank (which includes the numbers, as well as the action and special card ranks (reverse, wild, exc.) ).

A deck class would have an ArrayList <UnoCard> property and it's constructor would initialize a fresh deck.

A hand class that also has an ArrayList <UnoCards> where it gets said cards from the deck class.

A discard pile class, which contains the cards discarded and the current card in play.

A "board" class (haven't figured out a better name for it yet) which would determine/keep track of the number of players/hands, the turn order, the locations of the cards, and the winning condition.

Area of confusion and concern I'm having:

From what I've read, I want to avoid circular dependency. So if that's the case, when a card type effects the state of a "hand" or the turn order or really anything else, then in what class do I place the method(s) that effect that? If I place it in the specific card class, wouldn't that create a circular dependency? So would it be better then to have the hand class figure out what can be done with a specific card and what that specific card effects (which wouldn't that hinder the cohesion of the class?)?

I was also thinking a possible solution might be to have the non-generic card types contain methods that return values as apposed to manipulating higher level classes, such as a boolean drawCards which returns true if cards need to be drawn, false otherwise (same for skip, reverse, exc.).Then maybe the board class can determine what to do if those values are true or false (which actually seems more convoluted since only one value would be allowed to be true at any given time).

The other solution I was considering is to have a single UnoCardRules class, which serves the sole function of providing methods to determine the effects of each card, that way each card class can only worry about defining the card's state.

View Replies View Related

Each Bar In Graph Will Represent Value Of Each Element Of Array

Nov 5, 2014

So I have an array which holds 19 elements, each element represents a value of 'income'. I'm trying to code the graph so that each bar will represent the value of each element of the array (income). I have been given the code ' for (int Bar = 0; Bar < array of values.length; bar++);' however i'm unsure if this is how to do it, or what to add to this code to make it work.

View Replies View Related

How To Split And Then Represent Splitted Sentence

Apr 12, 2015

how to split and then represent the splitted sentence.Suppose I have:

Java Code:

String sentence = "I Love my mother <=> I Love my father";
String[] array = sentence.split("->"); mh_sh_highlight_all('java');

But how can I get now the lefthandside String values which is "I love my mother" and righthandside "I love my father"? I tried smth like:

Java Code:

String[] leftHandSide = array[0].split(" ");
String[] rightHandSide = array[1].split(" "); mh_sh_highlight_all('java');

But it's not working - getting error.

View Replies View Related

Creating Instances To Represent Deck Of Cards

Feb 12, 2014

I am creating a deck of 52 cards each with their rank(1 2 3...King, Ace) and suit(Clubs... Spades).

I want to be able to print out value of each card after creating them such as - 2 of CLUBS and so on. I am able to do that now. Only problem is, when it comes to the picture cards, I am representing them with numbers 11(Jack), 12(Queen) 13(King) and 14(Ace). I can't quite get around to changing the numbers to be represented as the actual words (King and so on).

I imagine that I should store the jack,queen,king,ace values in an enum too and call it in my loop but can't quite get it. Should I have like a 3rd for loop in my following codes to make it work?

Java Code:

//My main method. The Card class follows below

public static void main(String[] args) {
Card[] cards = new Card[52];
int i = 0;
for (Card.Suits suit : Card.Suits.values()) {
for (int y = 2; y < 15; y++) {
cards[i] = new Card(y, suit.name());
i++;

[Code] .....

View Replies View Related

How To Represent Message For Elgamal Elliptic Curve In Java

May 3, 2014

I am working on my project that uses elgamal elliptic curve. I know when the elgamal ec encrypt by following steps

Represent the message m as a point M in E(Fp).
Select k ∈R [1,n−1].
Compute C1 = kP.
Compute C2 = M +kQ.
Return(C1,C2).

Where Q is the intended recipient’s public key, P is base point. My qusetion at number one.How represent m as a point. Is point represent one character or represent group of characters. also I need code by java done this issue like Koblitz Encoding Method for ECC

View Replies View Related

Generate Random Number Between 0 And 19 Which Represent Location In Array

Aug 2, 2014

To simulate it, the program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the 3 numbers to the left and right of this location should be reset to the value 0. If there isn't 3 numbers to the left and right you may assume a lesser number depending on the boundaries of the array.

How to reset the numbers to 0 in the final array ?

View Replies View Related

DiceStatistics - Represent Pair Of Dice And Totals Of Their Rolls

Apr 19, 2014

Create a class called DiceStatistics to represent a pair of dice and the totals of their rolls. It should have an array of two Dice as a private data member (these are the Dice from Problem 1). It should also have, as a data member, an array of integers to represent the possible totals of rolling two dice.

The class should also have the following methods:

initStats() to initialize all the totals to zero.
rollOnce() to roll each die once, and add one to the correct totals element
printStatistics() to print the number of times each total has come up over the number of runs.

Here is my code:

public class DiceStatistics {
private final int NUMBER_OF_DICE = 2;
private Dice[] dice = new Dice[NUMBER_OF_DICE];
private int[] totals;
private int numberOfRolls;

[Code] ....

And here is the error:

run:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: Dice.roll
at DiceStatistics.rollOnce(DiceStatistics.java:27)
at DiceStatistics.main(DiceStatistics.java:55)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

I know it has something to do with the fact that I need to somehow import the information from my first program "Dice" into this in order to actually get the dice statistics, but how do I do that?

View Replies View Related

Represent Time Span Of Hours And Minutes Elapsed - Method Is Undefined For Type TEST

Jul 29, 2014

I am trying to run a class with a client code and I get the following error

The method add(TimeSpan) is undefined for the type TEST

Why i get this error?

package timespan;
// Represents a time span of hours and minutes elapsed.
// Class invariant: minutes < 60
public class TimeSpan {
private int hours;
private int minutes;

[Code] ....

View Replies View Related

JavaFX 2.0 :: 3D Sphere To Represent Each Single Point - Rotation Lagging With Large Number Of Points

May 22, 2014

We are doing a visualisation tool for point cloud research project. We use 3d sphere to represent each single point and when we have large number of points to display (~40,000), the rotation becomes very lagging.
 
What we have tried:

set JVM flag -Djavafx.animation.fullspeed=true, this worked a bit, but not significant.set JVM flag -Djavafx.autoproxy.disable=true, this did not work.

set Cache to true and CacheHint to Cache.SPEED, this did not make much difference.create another thread to do the rotation, and sync back after calculation, this did not work neither.

View Replies View Related

Rectangle Keeps Getting Faster?

Apr 12, 2014

obstacleX = (int) (obstacleX - 0.1);
if(obstacleX <= 0){
obstacleX = 600;
}
repaint();

Thats my code, and the rectangle (ObstacleX is the X cordinate for the rectangle) goes fine on the first few times across the screen, then starts to go hyperspeed....

View Replies View Related

Bounding Of A Rectangle

Sep 17, 2014

I have to do the following: A bounding rectangle is the minimum rectangle that encloses a set of points in a two-dimensional plane. Write a method that returns a bounding rectangle for a set of points in a two-dimensional plane, as follows:

public static MyRectangle2D getRectangle(double[][] points)

The Rectangle2D class is defined in Programming Exercise 10.13. Write a test program that prompts the user to enter five points and displays the bounding rectangle's center, width, and height. Here is a sample run:

Enter five points: 1.0 2.5 3 4 5 6 7 8 9 10.The bounding rectangle's center (5.0, 6.25), width 8.0, height 7.5

This is my code so far, taking in account that Rectangle2D is already done in a previous problem.the thing is that i don't know if i have to erase public static void or do i leave it or how do i start it?

package theboundingrectangle;
import java.util.Scanner;
public static MyRectangle2D getRectangle(double[][] points)
public class TheBoundingRectangle {
public static void main(String[] args) {
// TODO code application logic here
}
}

View Replies View Related

How To Get Rectangle From JScrollPan

Apr 21, 2015

in order to speed up my application I want 'paintComponent(Graphics g)' to only write the visible area in my viewport. I can not find how to get that Rectangle from my JScrollPane.

View Replies View Related

How To Design Classes

Mar 29, 2014

design a class to conduct a survey of three hospitals. you want to know how many sectors (eg operation, children, gastronomic) each hospitals have, and how many doctors there are in each sector.

what is the process of class design?

View Replies View Related

Rectangle Won't Display In JFrame

Jan 30, 2014

So I've been getting back into Java and downloaded eclipse back onto my laptop. However when I go to make a simple rectangle into a JFrame, the frame will pop up but no rectangle will be shown. Here is my main class, which sets up the JFrame...

mport javax.swing.JFrame;
public class Frame{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("I hope this fucking works");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

[code]....

View Replies View Related

Swing/AWT/SWT :: Add A Rectangle To JFrame?

Apr 13, 2014

Is there a way to simply slap a rectangle into a JPanel (make it appear) with out creating an inner Class or helper Method, all within "Main"? And if not, why?

Making a JFrame is easy.

Adding a JPanel is a snap.

import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TheJFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub

[code]....

View Replies View Related

Rectangle Isn't Showing Up In JFrame

Nov 14, 2014

MyGraphics worked before I added a background but, even now when I take the background away it isn't showing up.

package com.snow.game;
import javax.swing.*;
import java.awt.*;
class MyGraphics extends JComponent { //creating a class for graphics
public void draw(Graphics g){
//calling Graphics making a new graphics (g) now you can use it to make objects
g.drawRect(10, 10, 50, 50); //Draws a rectangle

[Code] ......

View Replies View Related

Creating A Word Rectangle?

Nov 7, 2014

I was tasked with building a program that, when is given a string by the user, takes it and prints it out as a rectangle. For example, if the user types in "COMPUTER", the output would be:

COMPUTER
OMPUTERC
MPUTERCO
PUTERCOM
UTERCOMP
TERCOMPU
ERCOMPUT
RCOMPUTE

So far, I have created 2 separate strings, and have gotten the output to look like:

COMPUTER
OMPUTERC
OMPUTERO
OMPUTERO
OMPUTERO
OMPUTERO
OMPUTERO
OMPUTERO
OMPUTERO

So, it works once, but then it doesn't work again. Here is my code:

i was tasked with building a program that, when is given a string by the user, takes it and prints it out as a rectangle. For example, if the user types in "COMPUTER", the output would be:

COMPUTER
OMPUTERC
MPUTERCO
PUTERCOM
UTERCOMP
TERCOMPU
ERCOMPUT
RCOMPUTE

So far, I have created 2 separate strings, and have gotten the output to look like:

COMPUTER
OMPUTERC
OMPUTERO
OMPUTERO
OMPUTERO
OMPUTERO
OMPUTERO
OMPUTERO
OMPUTERO

So, it works once, but then it doesn't work again. Here is my code:

import java.util.Scanner;
public class WordRectangle {
  /**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner userInput = new Scanner (System.in);

[code]....

View Replies View Related

Creating Rectangle In Java

Apr 16, 2015

I have a problem with creating a rectangle in Java. When I create the first rectangle all is ok. But the next, is the problem.

View Replies View Related

Draw Rectangle Through ArrayList

Oct 30, 2014

So I also am working on this problem. My frame does print each new rectangle; however, it is printed each time from the top left corner, and the bottom right is where I click.

Here is my RectangleComponent:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Shape;
import java.util.ArrayList;
import javax.swing.JComponent;
public class RectangleComponent extends JComponent

[Code] ....

View Replies View Related

Head First Design Pattern

Jan 11, 2014

I was reading head first java and the author told to read head first design pattern next.

View Replies View Related

Java Design Patterns

May 9, 2015

we have been given a scenario to design a system in which we have to make the class diasgrams. however we have to use appropriate patterns that match the scenario.

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved