![]() |
Lab 11
|
System.out.println()
and System.out.print()
.
To get started: Download the lab11_programs
folder
from the cs111d
account.
System.out.print()
vs.
System.out.println()
(a reminder)print()
and println()
are two methods
that allow you to print text to the Java console window.
print()
prints your text and leaves
the cursor at the end of printed text, ready to print the next
thing on the same line.
println()
, on the other hand,
prints your text and then waits at the start of the next line to
print the next thing.
This is best demonstrated by example. Each snippet of code produces the window to the right.
|
(Note: no spaces between |
|
|
|
|
labArray.java
in the folder labArrays
in
lab11_programs
. There are two different ways that
you can view some examples of how printArray()
and
reverseArray()
work:
printArray()
and reverseArray()
are invoked from the
TestlabArray
class like this:
TestlabArray.printArray(a);
. This means that Java is
using the version of printArray()
from the TestlabArray
class. If
you peek in the labArrays
folder, you'll see the
TestlabArray.class
file (but no
TestlabArray.java
file, which would contain the
answers).
TestlabArray.printArray(a);
We want to be able to print out the contents of our arrays (so
that when we test our array methods, we can print out the arrays to
make sure that the methods are working). Let's write a method
called printArray(int [] arr)
that prints out the
contents of the integer array arr on one line. For example, say we
have an array B
that has the value [2, 4, 6,
8
. Then invoking printArray(B)
should produce
the following in the Java Console window:
[2, 4, 6, 8]
for
loop to traverse the array.
Now we want to reverse the contents of our array. So, if array
C
is [0, 1, 2, 3, 4]
, and we run
reverseArray(C)
, then C
's value is
[4, 3, 2, 1, 0]
. When we have reversed an array, we
can use printArray()
to see the result! In your
main()
method, print out the value of each array
before and after invoking reverseArray()
to check that
your reverseArray()
actually works. Our test code
looks like this:
int[] a = { 9, -1, 3, 6, -10}; // creates an array named a
System.out.println("Original a ");
printArray(a); // print out the original array
System.out.println("Reversed a ");
reverseArray(a); // reverse the array
printArray(a); // print out the reversed array
System.out.println("--------");
Important Thing to Know reverseArray()
must change
the order of elements of the given array. It does not do any
printing itself (you may, of course, insert print statements for
debugging, and remove or comment out them later).
Some background:
How to open a file (that you plan on writing into)? |
FileWriter writer = new FileWriter("myNewFile");
|
How to open a file (that you plan on reading from)? |
BufferedReader reader = new BufferedReader(new FileReader(fileToReadFrom));
|
How to get one line read from a file? |
reader.readLine();
|
How to write to a file? |
writer.write("atch atch atchoooooooooooo!");
|
How to close a file? |
writer.close();
|
How to try and catch? |
try { //opening files and manipulating them goes here
|
To run your program, type this in the Java Interactions pane:
java LineNumber spaghetti.txt
java
: executes a java program
LineNumber
: the name of the program you want to execute
spaghetti.txt
:
the name of the file that your program will read from
If you forget to tell drJava which file to read from, it will
give you an error message like this:
If your program runs correctly, your console output should look like this:
One quirk of drJava is that drJava assumes that any files being
read from or written to are in the same folder as the drJava application.
We usually like to read from and write to files that are in a folder on
our desktop. Hence, the following code sets the path correctly for
the files that we will use in today's lab:
URL u = LineNumber.class.getResource("/LineNumber.class");
String path = new File(u.toString()).getParent();
String newpath = path.substring(5,path.length());
newpath = newpath + "/";
The code above sets the string variable newpath to contain the path
to your LineNumber.class file. For example, newpath on Sohie's
computer is this:
/Users/slee/Desktop/LineNumber
We want all files that LineNumber.java uses or produces to be contained
within the LineNumber folder.
If you don't use newpath
, then drJava is unable to find
the spaghetti.txt file. Similarly, if you don't use newpath
,
any files that you create and write to will not be in the LineNumber folder.
In order to do this, you need to use the split
method from the String
class. split
splits a given string into parts, given a delimiter, and places
those parts into an array. For example:
Produces this output:
String song = "Old man Dan he's a mean old man";
String alphabet = "a-b-c-d-e-f-g-h-i-j-k-l-m-n-o-p-q-r-s-t-u-v-w-x-y-z";
String [] songwords = null;
String [] letters = null;
songwords = song.split(" "); // delimiter is a space
letters = alphabet.split("-"); // delimiter is a dash
for (int i = 0; i< songwords.length; i++) {
System.out.println(songwords[i]);
}
System.out.println("***");
for (int i = 0; i< letters.length; i++) {
System.out.println(letters[i]);
}
Old
man
Dan
he's
a
mean
old
man
***
a
b
c
d
e
.
.
.
It's even more useful to do this to actual java programs:
Before: | |
![]() |
After: |
![]() |
Notes:
|
|
|
Your task is to amend LineNumber.java so that it creates a new file with a right-aligned line number comment at the start of each line. Assume that each line number is allowed a max of 4 digits (in other words, no programs over 9999 lines).
Hint: the number of spaces needed for a number n is log base 10 (n).
In Java, log base 10 (n) is done like this:
(int) (Math.log(n)/Math.log(10));