Lab 03

PLEASE NOTE: All the lab exercises are due Monday, May 26th by 10:00am.
  1. What you should know
  2. Using the Scanner class with our Board
  3. String manipulation
  4. Concatenation exercise
  5. Scanner Practice
  6. Request lab solutions

What you should know

This lab covers lectures 4 and 5. You are expected to know about the String class and Scanner class. A quick overview of what you should remember is given here.

Using the Scanner class with our Board

Create the file Square.java in DrJava. Your task is to draw a square on a 2-D Board. In the main method you should write code (using the Scanner class) to:

For example, if the input was

	4 7
	0 1 3

OR

 
	4 7 0 1 3

OR

 
	4 
	7 
	0 
	1 
	3

(Think about why this will work either way)

the output would be

You may assume the dimensions of the board specify a legal 2-D board. You may assume that the square will be able to fit on the board.

Submit your code for this in the box below.


String manipulation

Create the file StringManipulation.java in DrJava. In the main method you should write code (using the Scanner and String classes) to:


For example, if the input was

	Einstein was born in Germany in 1879

the output would be

	Einstein was born in Canada in 1879

Note: There is exactly one space between all the words.

You may assume there is at least one occurrence of the word "Germany" in the line of text. You may also assume Germany will NOT be the last word in the sentence. You should also note that the String class has (yet another) indexOf method that has a String parameter and returns the position of the first occurrence of the parameter String in the original String. There is also an indexOf method that takes a String and an integer, and behaves in the same way as the character and integer version of indexOf. To view helpful information and documentation regarding the String class, click here.

Submit your code for this in the box below.


Concatenation exercise

Write the output of the following lines of code WITHOUT running them in DrJava (You may check your answers in Dr.Java however it is crucial you do this exercise without Dr.Java first. This is similar to a midterm or a final exam question where you will not have a computer, and will only be able to rely on your own knowledge.):

int a = 42;
int b = 7;
double c = 3.14;
String s = "45";
String t = "18";
String u = "";

System.out.print( a + b );
System.out.print( " " );
System.out.print( a + t );
System.out.println( );
System.out.println( a + c );
System.out.println( c + a );
System.out.println( s + t );
System.out.println( s + b );
System.out.println( s + c );
System.out.println( a + s + b );
System.out.println( s + a + b );
System.out.println( a + b + s );
System.out.println( u + a + b );

Submit your answers below. (Please note that the format of your answers should reflect exactly what DrJava would output. HINT: Pay attention to print vs println.)


Scanner Practice

Create a file ScannerPractice.java that does the following:
- Asks for three integers on one line.
- Asks for your name on a new line.
- Outputs the text "Chris owes Steven $1000000." where your name should replace "Chris" and the sum of the three integers should replace "$1000000".

An example of the expected output is given below.

Enter three integers on one line.
123 456 789
Enter your name on a new line.
Chris
Chris owes Steven $1368.

Submit your code for this in the box below.


Request lab solutions

If you request solutions for this lab you will be sent an email that contains your answers as well as the expected answers. The email will be sent out to all interested students once the deadline for completing the exercises has passed for all students.