CSci 2101 Data Structures: Lab 1
Before you start, please read A very brief introduction to Linux. Please ignore the description of scp.
Tasks marked with * are extra credit. Work on these tasks only if you
are not pressed for time.
Problem 1
For this problem please create a new file LabOneProbOne.java and
copy/paste the program below into it:
import java.io.*;
public class LabOneProbOne {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// The code below reads one integer n
// prompt the user:
System.out.print("Please enter an integer: ");
// read a line of input:
String line = in.readLine();
// extract the integer from the entered data:
int n = Integer.parseInt(line);
// fill in your code here:
// print out the value of n
// increment n by 1
// print out the new value of n
System.out.println("Done!");
}
}
// What happens if the input is not an integer?
- Task 1: Compile and run the program. When it asks for an
integer, enter any integer number (a negative one is OK) and press
Enter. The program should print
Done!
- Task 2: What happens if you enter something other than an
integer? Try it and write the answer in comments after the program.
- Task 3: Fill in the code according to the comments in the
file.
- *Task 4*: Can you print the new and the old value of n on one
line without changing the order of the statements in the program? The
string
Done!
should still be on a separate line.
Submit the file to your Wiki lab page.
Problem 2
Each of the following two programs have several compiler
errors. Copy/paste the programs into the appropriate java files and
get them to compile and run.
public class LabOneProbTwo {
public static void main(String[] args) {
Int x = 5.0
double y = 2.4;
y = x + 3.3;
System.println("x = " + x + " y = " + );
}
}
public clas LabOneProbThree {
public void main(String[] args) {
int myVariable = 55;
myOtherVariable = myVarible + 55;
double dVariable = myVariable * 2.3 - myOtherVariable / 3.;
System.out.print("Here is the result: );
System.out.println(dVariable;
}
Submit both files to your Wiki lab page.
Problem 3
Which of the following are legal variable names in Java? Test each of
the names in a program. Write your answers on your Wiki lab page
(under the header Lab 1 Problem 3). Submit the program that you used
to test these names together with the other files for this lab.
- r2d2
- noWay!
- 2BeOrNot2Be
- I love Java
- I-love-Java
- I_love_Java
- string
- switch
- _five_
- _5_
- anExtremelyAndAnnoyinglyLengthyVariableNameWhichIsTerriblyHardToRead
This is a lab from CSci 2101 course.