|
|
Please, make sure that you have added the honor code statement at the top of your Java file:
/* THIS CODE IS MY OWN WORK, IT WAS WRITTEN WITHOUT CONSULTING CODE WRITTEN BY OTHER STUDENTS. _Your_Name_Here_ */ |
The deadline for completing this lab assignment is midnight, today.
mkdir ~/cs170/lab8 cp ~cs170002/share/lab8/* ~/cs170/lab8 cd ~/cs170/lab8 |
In this lab, we will use gedit and javac (not BlueJ !).
(You could use BlueJ, but you must figure out how to compile and run the program on your own.)
gedit Scoping.java & |
The variable in the program have been high lighted in the following diagram:
public class Scoping { static double a = 123; /* ....................... line (1) */ public static void F( double a ) /* ....................... line (2) */ { System.out.println("------ inside F( double a ) !!!!!!!!!!!!!!!!"); System.out.print("a at (2) = "); // TODO: Write a System.out.println() statement that prints a on line (2) System.out.print("a at (1) = "); // TODO: Write a System.out.println() statement that prints a on line (1) } public static void F( String a ) /* ....................... line (3) */ { System.out.println("------ inside F( String a ) "); System.out.print("a at (3) = "); // TODO: Write a System.out.println() statement that prints a on line (3) System.out.print("a at (1) = "); // TODO: Write a System.out.println() statement that prints a on line (1) } public static void main( String[] args ) { double x = -444444444; /* ....................... line (4) */ // TODO: Write a method call F() that passes x at line (4) F(x); // I have given you the answer for the first problem. { double a = 9999; /* ....................... line (5) */ // TODO: Write a method call F() that passes a on line (5) // TODO: Write a method call F() that passes a on line (1) } // TODO: Write a method call F() that passes x at line (4) // TODO: Write a method call F() that passes a on line (5) { String a = "9999"; /* ....................... line (6) */ // TODO: Write a method call F() that passes x at line (4) // TODO: Write a method call F() that passes a on line (6) // TODO: Write a method call F() that passes a on line (1) } } } |
Notice that:
|
javac Soping.java java Scoping |
gedit Scoping & |
public static void F( double a ) /* ....................... line (2) */ { System.out.println("------ inside F( double a )"); System.out.print("a at (2) = "); // TODO: Write a System.out.println() statement that prints a on line (2) System.out.print("a at (1) = "); // TODO: Write a System.out.println() statement that prints a on line (1) } |
javac Scoping.java java Scoping |
and if correct, you should see this output:
------ inside F( double a ) a at (2) = -4.44444444E8 a at (1) = 123.0 |
|
{ double a = 9999; /* ....................... line (5) */ // TODO: Write a method call F() that passes a on line (5) // TODO: Write a method call F() that passes a on line (1) } |
If it is not possible to write the requested method call, then write the following statement in its place:
System.out.println("Impossible: Write a method call F() that ... "); |
javac Scoping.java java Scoping |
|
// TODO: Write a method call F() that passes x at line (4) // TODO: Write a method call F() that passes a on line (5) |
If it is not possible to write the requested method call, then write the following statement in its place:
System.out.println("Impossible: Write a method call F() that ... "); |
javac Scoping.java java Scoping |
|
public static void F( String a ) /* ....................... line (3) */ { System.out.println("------ inside F( String a ) !!!!!!!!!!!!!!!!"); System.out.print("a at (3) = "); // TODO: Write a System.out.println() statement that prints a on line (3) System.out.print("a at (1) = "); // TODO: Write a System.out.println() statement that prints a on line (1) } |
{ String a = "9999"; /* ....................... line (6) */ // TODO: Write a method call F() that passes x at line (4) // TODO: Write a method call F() that passes a on line (6) // TODO: Write a method call F() that passes a on line (1) } |
If it is not possible to write the requested method call, then write the following statement in its place:
System.out.println("Impossible: Write a method call F() that ... "); |
javac Scoping.java java Scoping |
|
Original string: "c0c1c2.....cn-3cn-2cn-1" Folded string: "c0cn-1c1cn-2c2cn-3....." |
In other words:
|
Original string ------> Folded string "ab" "ba" "abc" "acb" "abcd" "adbc" "abcde" "aebde" "abcdef" "afbecd" and so on... |
Use 2 pointer (indices): i points at the start and j points at the end Use a variable named "output" to collect the folded string |
|
Use these command to compile and test:
javac TestFoldString1.java java TestFoldString1 |
Correct output:
s = a, folded = a correct s = ab, folded = ab correct s = abc, folded = acb correct s = abcd, folded = adbc correct s = abcdegh, folded = ahbgced correct s = abcdefghi, folded = aibhcgdfe correct |
cd ~/cs170/lab8 /home/cs170002/turnin-lab Scoping.java lab8 /home/cs170002/turnin-lab Fold.java lab8a |
before midnight today.
For the Lead TA:
|
|
Use 1 pointer i Use 2 variables to collect to unfolded string: out1 contains the prefix of the unfolded string out2 contains the postfix of the unfolded string |
Use these command to compile and test:
javac TestFoldString2.java java TestFoldString2 |
Correct output:
s = a, unfolded(fold(s)) = a correct s = ab, unfolded(fold(s)) = ab correct s = abcdefgh, unfolded(fold(s)) = abcdefgh correct s = abcdefghi, unfolded(fold(s)) = abcdefghi correct |