CS 101 Laboratory #7 Lab prep: Read this lab assignment and review the slides and examples from lectures 18-21. Additional information can be found in J 3.3, 3.4, 6.4, and 8.
Written Exercises: None this week.
this is a 3 line file with eleven words in it. > java WordCount Usage: java WordCount <file name> > java WordCount test.text Error: can't find file test.text > java WordCount test.txt Number of words in test.txt: 11Test your program with different input files. If you want to check your results, here is a sample application that you can save in your hw07 folder: WordCountSample.class.
Submit a copy of your Java source code for WordCount.java along with some sample output (you can print text from the Interactions pane by right-clicking in it and selecting "Print Interactions").
Let's move on to object-oriented programming. You will be modifying BankAccount.java, which defines a class very similar to the Account class from lecture 19. First, save the BankAccount.java file to your hw07 folder, load it into DrJava, and compile it. In the Interactions pane, experiment with creating BankAccount objects and issuing commands to them. For example, you might interact with the Java interpreter as follows:
> BankAccount mary = new BankAccount (7, 100); > mary Account #7 > mary.deposit(50.25); Deposited $50.25. > mary.withdraw(200); Sorry, you must withdraw a positive amount of money up to $150.25. > mary.withdraw(140); Withdrew $140.0. > mary.printBalance(); Your balance is $10.25.Now, modify the code so that a password is a required argument for creating an account, as well as for invoking the withdraw and printBalance methods. A sample transcript of how the modified BankAccount class should behave after making the changes for the previous exercise and this one is shown here:
> BankAccount jane = new BankAccount(135, "apple", 2000);
> BankAccount john = new BankAccount(277, "pear", 500);
> jane.deposit(50);
Deposited $50.0.
> jane.deposit(-80);
You must deposit a positive amount of money.
> jane.printBalance("aapl");
Sorry, incorrect password.
> jane.printBalance("apple");
Your balance is $2050.0.
> john.withdraw(200, "pare");
Sorry, incorrect password.
> john.withdraw(200, "pear");
Withdrew $200.0.
> john.withdraw(500, "pear");
Sorry, you must withdraw a positive amount of money up to $300.
> john.withdraw(300, "pear");
Withdrew $300.0.
> john.deposit(2000);
Deposited $2000.0.
> john.printBalance("pear");
Your balance is $2000.0.
If you want to check your results, here is a sample class file that
you can save in your hw07 folder:
BankAccountSample.class.
Turn in a printout of your source code, as well as a transcript (like the one above) showing that your code works correctly.
Develop a class called Buggle that implements these creatures. Your code should provide functionality for the following methods:
// construct a new Buggle public Buggle() // move forward one grid cell public void forward() // move forward 'k' grid cells public void forward(int k) // rotate 90 degrees to right (EAST -> SOUTH -> WEST -> NORTH -> EAST) public void right() // rotate 90 degrees to left (EAST -> NORTH -> WEST -> SOUTH -> EAST) public void left() // return a string representation of the object public String toString()
Write another class to test your Buggle class. For example, suppose the following code is in the main method of a class called BuggleTest:
// create a Buggle
Buggle becky = new Buggle();
// print it (this invokes the toString() method)
System.out.println("becky: " + becky);
// move becky around a bit and keep printing it
becky.forward(); System.out.println("becky: " + becky);
becky.left(); System.out.println("becky: " + becky);
becky.forward(5); System.out.println("becky: " + becky);
// create another Buggle and print it
Buggle bobby = new Buggle();
System.out.println("bobby: " + bobby);
// move bobby without printing
bobby.right();
bobby.forward(5);
bobby.right();
bobby.forward(20);
bobby.left();
// move becky some more
becky.left();
becky.forward(3);
// report final state of both Buggles
System.out.println("becky: " + becky);
System.out.println("bobby: " + bobby);
The output of this code would be:
> java BuggleTest becky: x=1, y=1, direction=EAST becky: x=2, y=1, direction=EAST becky: x=2, y=1, direction=NORTH becky: x=2, y=6, direction=NORTH bobby: x=1, y=1, direction=EAST becky: x=-1, y=6, direction=WEST bobby: x=-19, y=-4, direction=SOUTH
Write a BuggleTest class with similar functionality. Turn in a printout of your Java code for both classes: Buggle.java and BuggleTest.java. Also turn in a copy of your sample output.
If you want to check your results, here are sample class files that you can save in your hw07 folder: BuggleSample.class and BuggleTestSample.class.
> BankAccount jane = new BankAccount(135, "apple", 2050);
> BankAccount john = new BankAccount(277, "pear", 2000);
> LinkedAccount ron = new LinkedAccount(john, "pear", "grape");
> john
Account #277
> ron
Account #277
> ron.deposit(100);
Deposited $100.
> ron.printBalance("pear");
Sorry, incorrect password.
> ron.printBalance("grape");
Your balance is $2100.
> ron.withdraw(300, "grap");
Sorry, incorrect password.
> ron.withdraw(300, "grape");
Withdrew $300.
> ron.printBalance("grape");
Your balance is $1800.
> john.printBalance("pear");
Your balance is $1800.
Turn in a printout of your source code, as well as a transcript (like
the one above) showing that your code works correctly.
Back to Computer
Science 101 Home
Department of Computer
Science