










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











-^ Other users–^ Available memoryFil
-^ File allocation
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
public
class
RobotException
extends
Exception{
public
RobotException
{^ super(“Error:
Bad
robot!”;}
end
class
RobotException
} //
end
class
RobotException
public
class
RobotOnFireException
extends
RobotException{
end
class
RobotOnFireException
public
class
CrazedRobotException
extends
RobotException{
public
void
useRobot
(Order
order)
throws
RobotException
boolean
taskPerformed
=^ giveRobotOrder(Order
order);
i^
public
class
CrazedRobotException
extends
RobotException{
end
class
CrazedRobotException
if^ (!taskPerformed())
if^ (isRobotOnFire())
throw
new
RobotOnFireException();
} if^ (
b^
h^
d^ b
i^ ()
if^ (RobotIQ
{^ throw
new
CrazedRobotException();
} } //^ end
useRobot Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
Exception
in^
thread
“main”
java.lang.StringIndexOutOfBoundsException:String
index
out
of^
range:
at^ java.lang.String.charAt(Unknown
Source)
at^ Coordinate.getStatus
(Coordinate:
at^ Ship.getLocation
(Ship:
at^ GameBoard.checkTarget
(GameBoard.java:
at^ GameBoard.play
(GameBoard.java:
at^ Main.main
(Main.java:
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
try^
{ // potentially
risky
code
}^ catch
(Exception
e)^
//^ do
this
if^
it^ fails
try^
{ robot.useRobot(order); }^ catch
(RobotOnFireException
e)^
useFireExtinguisher(robot); }^ catch
(CrazedRobotException
e)^
startVirus(robot); } catch
(Exception
e)^
}^ catch
(Exception
e)^
System.out.println(“Robot
“is^
out^
of^ control.
Run!”;
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
public static void main (String[] args) {boolean validFile;do {
String filename = getFilenameFromUser();File fileHandle = new File (filename);Scanner inputFile = null;validFile =
true; try {^ inputFile = new Scanner (fileHandle); } catch (Exception e) {^ System.out.println("Error opening input file: " + filename);validFile = false; }} until (validFile);processFile(inputFile); } // end method main
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
}^ // end method main
import java.io.FileOutputStream;import java.io.DataOutputStream;… public void storeToFile(ArrayList
try {FileOutputStream fs = new FileOutputStream(filename);DataOutputStream output = new DataOutputStream(fs);for (int number : numberList) {for (int number : numberList) {
output.writeInt (number);} output.close ();
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
import java.io.FileInputStream;import java.io.DataInputStream;… public ArrayList
ArrayList
new FileInputStream(filename);
FileInputStream fs =
new FileInputStream(filename);
DataInputStream input = new DataInputStream(fs);while (!endOfFile) {numberList.add( input.readInt() );} } catch (Exception ex) {if (ex instanceof EOFException) { endOfFile = true; }else {ex.printStackTrace();}} input.close();input.close();
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
import java.io.RandomAccessFile;public class Main {public static void main (String[] args) throws Exception {
final int SIZE_OF_INT_IN_BYTES = 4;RandomAccessFile file =
new RandomAccessFile("Data.dat","rw");
file.writeInt(7000);file writeInt(102); file.writeInt(102);file.writeInt(123456);file.seek(1 * SIZE_OF_INT_IN_BYTES);System.out.println( file.readInt() );file.close();} // end method main } // end class Main
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
-^ The specific values of the instance variables (its state)–^ Stored in a specific order somewhere in the heap–^ With a reference that tells you where to find the structure
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
// class header and statepublic class Deckimplements Serializable {
// class methodspublic Deck () {for (String suit : suitList) {
static final String[] suitList ={"Club","Diamond","Heart","Spade"};static final String[] rankList =
for (String rank :
rankList) {
cardList.add(new card(rank,suit));} // for each rank } // for each suit} // end constructor
static final String[] rankList ={"Ace", "Two", "Three", "Four",“Five”, "Six", "Seven", "Eight","Nine", "Ten", "Jack", "Queen","King"};
} // end constructorpublic void showDeck () {for (Card card: cardList) {
System.out.println(card);
ArrayList
new^
ArrayList
} // for each card} // end method showDeck
bli^
i^
id^
i^ ( i^
[]^
) {
public static void main (String[] args) {
Deck deck = new Deck();deck.storeToFile("deckFile.dat");deck = deck.getFromFile("deckFile.dat");deck.showDeck (); Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II
();
} // end main
import java.io.Serializable;import java.io.FileOutputStream;import java.io.ObjectOutputStream;… public void storeToFile(String filename) {
Wright State University, College of EngineeringDr. T. Doom, Computer Science & Engineering
CS 241Computer Programming II