codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  java API and designing an implementing hierarchies  mrnewbiw at 12:41 on Saturday, October 08, 2005
 

I have a college assignment which I am having trouble with:
TASK 1
It is important to be able to find classes from the Java API that are useful to you and then adapt them to suit your needs by extending them. The following activity gives you practice at this.

(Horstmann P11.3)
a) Implement a subclass Square that extends the Rectangle class. In the constructor accept the x and y positions of the centre and the side length of the square. (Use the setLocation and setSize methods of the Rectangle class from the Java API).
b) Supply a method getArea that computes and returns the area of the square.
c) Write a test harness that creates a new Square object and then prints out its details using the toString method that has been inherited from the Retangle class.
d) Also print out the area of the Square.

TASK 2
You have been placed in charge of designing the University Administration System. Old versions were not implemented using an object oriented approach and you have been asked to come up with a prototype class design. Initially all that is required is a hierarchy that satisfies the following criteria:

a) The system contains Lecturers, UnderGradStudents and PostGradStudents.
b) Every Person in the system has a name.
c) The system also holds:
i) whether a Lecturer is at a senior level ‘S’ or a normal level ‘N’.
ii) the student number of each Student
iii) The fee structure of each Student (assume “Full Fee” for PostGradStudent, “HECS” for UnderGradStudent)
iv) A String outlining the previous qualification of a PostGradStudent
v) whether or not an UnderGradStudent wants to be part of the mentorship program (‘Y’ or ‘N’).

All the system is required to do initially is have accessor and mutator methods for each instance field and a toString method. The toString method returns a String that reports all the details relating to that class.

Design and implement what you believe to be the best possible class hierarchy you can for this system.
1. Sketch out a UML style class diagram.
2. Are you going to use abstract classes? Interfaces? Be ready to justify your decisions.
3. Do your best to construct your system. Of course you will need to create a test harness for your program.


  Re: java API and designing an implementing hierarchies  crwood at 22:08 on Saturday, October 08, 2005
 

import java.awt.*;

public class SquareTest
{
public static void main(String[] args)
{
Square square = new Square(20, 20, 10);
System.out.println("square = " + square + "\n" +
"area = " + square.getArea());
}
}

class Square extends Rectangle
{
int x, y, side;

public Square(int x, int y, int side)
{
this.x = x;
this.y = y;
this.side = side;
super.setLocation(x,y);
super.setSize(side, side);
}

public int getArea()
{
return side * side;
}
}

  Re: java API and designing an implementing hierarchies  Shaifaliaggarwal at 09:46 on Monday, May 22, 2006
 

Hi

hi

i need your help in using java comm aPI for windows XP platform ..
i have written a java prgm for writing and then reading on a serial port.
but after writing i want to confirm it by reading .
but the no , of bytes wriitten or read by input stream are dislayed as 0
please help me out for this problem.

i guess that the write opertaion is not succesfull in writing on serial port .
the event DATA_AVAILABLE IS NEVER fired
while the event OUTPUT_BUFFER_EMPTY is fired when i do the write operation .

Please have a look into the attached file
and please gave me some suggestion to overcome the same problem.
I am waiting for your response..

thanks.

import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import javax.comm.*;


/**
A Simple Program to write and then read at a serial port using java
comm api
*/
public class SimpleReadWrite implements Runnable,
SerialPortEventListener {

static String DEFAULT_PORT = "COM1";
InputStream in;
OutputStream out;



/**
* Main method
*/
public static void main(String[] args) {
String portname = DEFAULT_PORT;
Enumeration portList;
try {
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
{
CommPortIdentifier portId = (CommPortIdentifier)
portList.nextElement();
if (portId.getPortType() ==
CommPortIdentifier.PORT_SERIAL)
{
if (portId.getName().equals("COM1"))
{
System.out.println("COM1 Serial Port Found");
break;
}
System.out.println("Port "+ portId.getName() +" Found
which is serial");
}
else
System.out.println("Port "+ portId.getName() +" Found
which is parallel");
}

CommPortIdentifier portId =

CommPortIdentifier.getPortIdentifier(portname);
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
{
SimpleReadWrite srw = new SimpleReadWrite(portId);
}
else {
System.err.println(portname + " is not a serial port");
System.exit(1);
}
}
catch (NoSuchPortException e) {
System.err.println("No such port: " + portname);
System.exit(1);
}
catch (PortInUseException e) {
System.err.println("Port in use: " + portname);
System.exit(1);
}
}

/**
* Constructor.
*/
public SimpleReadWrite(CommPortIdentifier portId) throws
PortInUseException {
SerialPort serialPort = null;
try {
serialPort = (SerialPort) portId.open("SimpleReadWrite",
5000);
in = serialPort.getInputStream();

out = serialPort.getOutputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.notifyOnOutputEmpty(true);
serialPort.setSerialPortParams(115200,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
Thread readerThread = new Thread(this);
readerThread.start();

}
catch (IOException e) {
System.err.println("Problem opening streams");
System.exit(1);
}
catch (TooManyListenersException e) {
System.err.println("Too Many Listeners");
System.exit(1);
}
catch (UnsupportedCommOperationException e) {
System.err.println("Problem setting port parameters");
System.exit(1);
}

}


public void run() {
try
{
out.write("ABC12".getBytes(),0,"ABC12".getBytes().length);
}
catch(IOException e) {}
try
{
out.flush();
}
catch(IOException e) {}
try
{
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace(); }

}

/**
* Process serial port events. This method is required
* when implementing SerialPortEventListener
*
* @param event SerialPortEvent object to process
*/
public void serialEvent(SerialPortEvent event) {

System.out.println("Serial Event of type "+event.getEventType());
switch (event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
System.out.println("It is in output buffer empty");

try
{
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace(); }
int numOfBytes = 0;
try
{
numOfBytes = in.available();
}
catch(IOException e) {}

System.out.println("number of bytes"+numOfBytes);

byte[] readBuffer = new byte[numOfBytes];
try {
while (in.available() > 0) {
int numBytes = in.read(readBuffer);
}
System.out.print("Data is :"+new String(readBuffer));
}
catch (IOException e) {
}

try
{
in.close();
}
catch(IOException e) {}
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}

break;
case SerialPortEvent.DATA_AVAILABLE:
System.out.println("It is in Data Available");
break;

default:
System.out.print("Unknown SerialPortEvent type = " +
event.getEventType());
break;
}
}
}









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Help with 2 level tab menu
•  Decoding a string in Javascript
•  Re: Array within array
•  Hey all could some 1 help me ?? thanks
•  Re: Checking for a File that Does Not Exist (Yet)
•  mouse trailer
•  Problems in login using WWW::Mechanize
•  using javascript to embed videos
•  Re: really lost my syntax


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008