Question
Please help with this Java coding problem. Will be using jGRASP. Look at Hw1.java and at one-liners.txt. At five points in within Hw1.java there are
Please help with this Java coding problem. Will be using jGRASP.
Look at Hw1.java and at one-liners.txt. At five points in within Hw1.java there are instructions indicating what Java code must be inserted. Once the program is complete, it should run similar to this:
---------------------------------------- > java OneLiners Enter a command: s 42 Enter a command: 41 I have the world's largest seashell collection. I keep it scattered on beaches all over the world. Maybe you've seen some of it. Enter a command: 40 If toast always lands butter-side down, and a cat always lands on its feed, what happens if you strap toast on the back of a cat and drop it? Enter a command: 28 If you were going to shoot a mime, would you use a silencer? Enter a command: 3 It's hard for me to buy clothes, because I'm not my size. Enter a command: 0 If you didn't know me, would you think I'm a stranger? Enter a command: r Mixed this water myself. Two glasses of H, one glass of O. Enter a command: r Snakes have no arms. That's why they don't wear vests. Enter a command: r I installed a skylight in my apartment. The people upstairs are furious. Enter a command: q ---------------------------------------- In the above, 'r' indicates that a random item must be displayed 's' indicates that the number of one-liners read from the data file must be displayed (the size) a number indicates that the one-liner at a particular index must be displayed 'q' indicates that the program should stop (quit) Also, in the skeleton given, the file and class names do not match. One or both of these must be changed to make the program run. Turn in your solution. + The solution *MUST* contain header comments containing (1) The name or names of the programmers submitting the program (one or two student names). (2) A brief description of what the program does. + Attach a screenshot of your program running. Include a few specific one-liners, a few random one-liners, the size of the database, and quit. + Do not submit a screenshot of the entire screen--just of the program run itself.
One-liners.txt
If you didn't know me, would you think I'm a stranger?
On the other hand, you have different fingers.
When the trees blow back and forth, that creates wind.
It's hard for me to buy clothes, because I'm not my size.
Bought a cordless extension cord.
I took a lie detector test. No I didn't.
I live on a one-way dead end street. Don't know how I ever got there.
A friend of mine's in jail for counterfeit pennies.
They caught him because he had the head and the tail on
the wrong side.
Snakes have no arms. That's why they don't wear vests.
Unscented perfume, came in a little empty bottle.
Small claims court jester.
Mixed this water myself. Two glasses of H, one glass of O.
I got food poisoning today. I don't know when I'm gonna use it.
Ballerinas. Why are they always on their tiptoes? Why don't they
just get taller women?
I was walking down the street, and suddenly the prescription ran
out on my eyeglasses.
Next week I'm getting an MRI to find out whether I have
claustrophobia.
I like to listen to tapes of my ears ringing.
I'm writing an unauthorized autobiography.
I spilled spot remover on the dog. Now he's gone.
Shin: a device for finding furniture in the dark.
Everywhere is within walking distance if you have the time.
If warm air rises, Heaven could be hotter than Hell.
What happens if you get scared half to death twice?
It's a small world, but I wouldn't want to paint it.
Why are there five syllables in 'monosyllabic'?
I couldn't repair your brakes, so I made your horn louder.
I have an inferiority complex, but it's not a very good one.
When everything's coming your way, you're in the wrong lane.
If you were going to shoot a mime, would you use a silencer?
Eagles may fly, but weasels don't get sucked into jet engines.
A lot of people are afraid of heights. Not me. I'm afraid of widths.
If at first you don't succeed, skydiving definitely isn't for you.
It doesn't matter what temperature it is; it's always room temperature.
I installed a skylight in my apartment. The people upstairs are furious.
Last year I went fishing with Salvador Dali. He was using a dotted line.
He caught every other fish.
I'm writing a book. I'm almost finished. I numbered the pages. Now all
I have to do is fill them in.
I watched the Indy 500, and I was thinking that if they left earlier
they wouldn't have to go so fast.
The other night I was lying in bed, looking up at the stars, and I
wondered, "Where the hell is my roof?"
If it's a penny for your thoughts, and you put in your two cents worth,
someone, somewhere is making a penny.
When I was in school the teachers told me practice makes perfect, then
they told me nobody's perfect so I stopped practicing.
If toast always lands butter-side down, and a cat always lands on its
feed, what happens if you strap toast on the back of a cat and drop
it?
I have the world's largest seashell collection. I keep it scattered on
beaches all over the world. Maybe you've seen some of it.
HW1.Java
// IS 247-01 Homework 1
// Read and display one-liners from Steven Wright.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Random;
import java.util.Scanner;
public class OneLiners {
final private String[] db; // the one-liners
final private int n; // the number of one-liners
final static private Random prng = new Random();
public OneLiners(final String fileName) throws FileNotFoundException {
final Scanner in = new Scanner(new File(fileName));
db = new String[1024];
n = readDb(in);
in.close();
} // constructor
private int readDb(final Scanner in) {
int index = 0;
while (in.hasNextLine())
db[index++] = readEntry(in);
return index;
} // readDb()
private String readEntry(final Scanner in) {
StringBuilder sb = new StringBuilder(in.nextLine());
while (in.hasNextLine()) {
String line = in.nextLine();
if (line.length() == 0)
return sb.toString();
sb.append(' ');
sb.append(line);
} // while
return sb.toString();
} // readEntry()
public String get(final int index) {
//
// Insert code that returns the one-liner from db at the specified
// index.
//
} // get()
public int size() {
//
// Insert code to return the size of db (the number of non-null
// elements in the array)
//
} // size()
public static void main(String[] args) throws FileNotFoundException {
final OneLiners ol = new OneLiners("one-liners.txt");
final Scanner in = new Scanner(System.in);
String cmnd;
do {
System.out.println();
System.out.print("Enter a command: ");
cmnd = in.nextLine().trim().toLowerCase();
if (Character.isDigit(cmnd.charAt(0)))
//
// Insert code to call stringAtIndex() and to display the result
//
else if (cmnd.charAt(0) == 'r')
//
// Insert code to display a randomly-chosen item from the db
//
else if (cmnd.charAt(0) == 's')
//
// Insert code to dislay the number of one-liners in the db
//
} while (cmnd.charAt(0) != 'q');
} // main()
public static String stringAtIndex(OneLiners ol, String cmnd) {
final int index = Integer.parseInt(cmnd);
return ol.get(index);
} // stringAtIndex()
} // class OneLiners
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started