The Artima Developer Community
Sponsored Link

Java Answers Forum
Desperate Beginner would Love ur HELP...

3 replies on 1 page. Most recent reply: Aug 24, 2002 3:39 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Trina

Posts: 4
Nickname: tweet
Registered: Aug, 2002

Desperate Beginner would Love ur HELP... Posted: Aug 20, 2002 5:33 AM
Reply to this message Reply
Advertisement
Hi
I am kind of new to Java. Im taking a distance learning course and I am on Classes/Methods and Control Contructs. I am stuck on a question:

1. Design & implement a class ferry which models a cross-channel ferry. The class should model the following state:
* currentDrive - which can be neutral, forward or reverse.
* bowDoors - which can be open or close.

Methods are:
* closeDoors - this closes the bow doors if they are open, otherwise it displays and error message.
* openDoors - this opens the bow doors if they are closed provided the current drive is in neutralotherwise it displays an error message.
* engageDrive - this requires an integer argument (-1 reverse and 1 forward); if the bow doors are clsed it will engage the selected drive, otherwise an error message is displayed;
* disengageDrive - if neither reverse or forward is engage it will be disengaged, otherwise an error message will be displayed.

Thanks.............
*


Ratul

Posts: 5
Nickname: rats
Registered: Aug, 2002

Re: Desperate Beginner would Love ur HELP... Posted: Aug 22, 2002 2:39 AM
Reply to this message Reply
Where exactly you got stuck? If you can pin-point it -then maybe I can help you.

Trina

Posts: 4
Nickname: tweet
Registered: Aug, 2002

Re: Desperate Beginner would Love ur HELP... Posted: Aug 24, 2002 3:37 AM
Reply to this message Reply
Hi Ratul,

The whole question of states and methods is hard for me to comprehend. I really cannot begin to answer that question.

Cheers

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Desperate Beginner would Love ur HELP... Posted: Aug 24, 2002 3:39 PM
Reply to this message Reply
The code for this can be downloaded at:
http://www.quantumhyperspace.com/SourceBank/Ferry.java
***************************************

/* Ferry.java
* @author: Charles Bell
*
* Design & implement a class ferry which models a cross-channel ferry.
* The class should model the following state:
* * currentDrive - which can be neutral, forward or reverse.
* * bowDoors - which can be open or close.
* * Methods are:
* * closeDoors - this closes the bow doors if they are open, otherwise
* it displays and error message.
* * openDoors - this opens the bow doors if they are closed provided the
* current drive is in neutralotherwise it displays an error message.
* * engageDrive - this requires an integer argument (-1 reverse and 1 forward);
* if the bow doors are clsed it will engage the selected drive,
* otherwise an error message is displayed;
* * disengageDrive - if neither reverse or forward is engage it will be
* disengaged, otherwise an error message will be displayed.
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** Models a cross-channel ferry.
*/
public class Ferry extends JFrame implements ActionListener{

final int NEUTRAL = 0;
final int FORWARD = 1;
final int REVERSE = -1;
private int currentDrive = NEUTRAL;

final int OPEN = 0;
final int CLOSED = 1;
private int bowDoors = CLOSED;

private JLabel driveStatusLabel;
private JLabel bowdoorStatusLabel;

public static void main(String[] args){
Ferry ferry = new Ferry();
ferry.init();
}

public void init(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel ferryStatusPanel = new JPanel();
driveStatusLabel = new JLabel("Neutral");
bowdoorStatusLabel = new JLabel("Closed");
ferryStatusPanel.add(new JLabel("Drive Status: "));
ferryStatusPanel.add(driveStatusLabel);
ferryStatusPanel.add(new JLabel("Bow Door Status: "));
ferryStatusPanel.add(bowdoorStatusLabel);

JPanel bowDoorControlPanel = new JPanel();
JButton openButton = new JButton("Open Bow Doors");
JButton closeButton = new JButton("Close Bow Doors");
JButton exitButton = new JButton("Exit");
bowDoorControlPanel.add(openButton);
bowDoorControlPanel.add(closeButton);
bowDoorControlPanel.add(exitButton);
openButton.addActionListener(this);
closeButton.addActionListener(this);
exitButton.addActionListener(this);

JPanel driveControlPanel = new JPanel();
JButton reverseButton = new JButton("Reverse");
JButton neutralButton = new JButton("Neutral");
JButton forwardButton = new JButton("Forward");
driveControlPanel.add(reverseButton);
driveControlPanel.add(neutralButton);
driveControlPanel.add(forwardButton);
reverseButton.addActionListener(this);
neutralButton.addActionListener(this);
forwardButton.addActionListener(this);

getContentPane().add(ferryStatusPanel, BorderLayout.NORTH);
getContentPane().add(bowDoorControlPanel, BorderLayout.CENTER);
getContentPane().add(driveControlPanel, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
show();
}

/** Closes the bow doors if they are open, otherwise it displays an error message.
*/
public void closeDoors(){
if (bowDoors == OPEN){
bowDoors = CLOSED;
}else{
JOptionPane.showMessageDialog(this, "The Bow Doors are already closed.",
"Error", JOptionPane.ERROR_MESSAGE);
}
}

/** Opens the bow doors if they are closed provided the current drive is in neutral.
* Otherwise it displays an error message.
*/
public void openDoors(){
if ((bowDoors == CLOSED) && (currentDrive == NEUTRAL)){
bowDoors = OPEN;
}else{
if (bowDoors == OPEN) {
JOptionPane.showMessageDialog(this,
"The Bow Doors are already open.",
"Error", JOptionPane.ERROR_MESSAGE);
} else if (currentDrive != NEUTRAL) {
JOptionPane.showMessageDialog(this,
"The Cuurent Drive must be in neutral to open the doors.",
"Error", JOptionPane.ERROR_MESSAGE);
}
}
}

/** Requires an integer argument (-1 reverse and 1 forward);
* If the bow doors are closed it will engage the selected drive,
* otherwise an error message is displayed;
*/
public void engageDrive(int request){
if (bowDoors == CLOSED){
if (request == -1) currentDrive = REVERSE;
if (request == 1) currentDrive = FORWARD;
}else{
JOptionPane.showMessageDialog(this,
"The Bow Doors must be closed to engage the drive.",
"Error", JOptionPane.ERROR_MESSAGE);
}
}

/** If neither reverse or forward is engage it will be disengaged, otherwise an error
* message will be displayed.
*/
public void disengageDrive(){
if (currentDrive == REVERSE) {
currentDrive =NEUTRAL;
}else if (currentDrive == FORWARD) {
currentDrive =NEUTRAL;
}else{
JOptionPane.showMessageDialog(this,
"The Drive was already disengaged.",
"Error", JOptionPane.ERROR_MESSAGE);
}
}

public void updateStatus(){
if (currentDrive == REVERSE) driveStatusLabel.setText("Reverse");
if (currentDrive == FORWARD) driveStatusLabel.setText("Forward");
if (currentDrive == NEUTRAL) driveStatusLabel.setText("Neutral");
if (bowDoors == OPEN) bowdoorStatusLabel.setText("Open");
if (bowDoors == CLOSED) bowdoorStatusLabel.setText("Closed");
}

public void actionPerformed(ActionEvent actionEvent){
String actionCommand = actionEvent.getActionCommand();
if (actionCommand.compareTo("Open Bow Doors") == 0){
openDoors();
}else if (actionCommand.compareTo("Close Bow Doors") == 0){
closeDoors();
}else if (actionCommand.compareTo("Forward") == 0){
engageDrive(FORWARD);
}else if (actionCommand.compareTo("Reverse") == 0){
engageDrive(REVERSE);
}else if (actionCommand.compareTo("Neutral") == 0){
disengageDrive();
}else if (actionCommand.compareTo("Exit") == 0){
System.exit(0);
}
updateStatus();
}

}

Flat View: This topic has 3 replies on 1 page
Topic: well doubt in packages.... Previous Topic   Next Topic Topic: Jsp import my own class HELP

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use