Lab 1: Intro to Java
Due: Thursday, Jan. 28, 11:59pm
The goal of this lab is to become familiar with writing Java code, including class structure, primitive types (mainly int), loops, and our first data structure, arrays. Before starting this lab, make sure you have finished Lab 0 and submitted your work on the server.
Part 1: Variables and Methods
NumberFacts.java
either using the Eclipse procedure described in Lab 0, or using another editor.
# Sample program to demonstrate variable types def main(): # ask user for a number n = int(input("Please enter an integer: ")) # double the number ndoubled = n+n print(n, "doubled is", ndoubled) # square of number nsquared = n*n print(n, "squared is", nsquared) # square root of number nsqrt = n**(0.5) print("The square root of", n, "is", nsqrt) if __name__ == "__main__": main()We are going to convert this code into Java, step by step the way we built up AddTax.java during the first class. In Eclipse, the class name will be set up for you. To set up the Javadocs and main, you can use this template:
/** * TODO: Add a description of the program here. * * @author TODO: Your Name Here * @version TODO: Today's Data Here */ class NumberFacts { /** * TODO: Description of what main does here. * * @param args Command line arguments */ public static void main(String[] args) { // TODO: convert Python code above into Java here } }
// ask user for a number System.out.print("Please enter an integer: "); Scanner input = new Scanner(System.in); int n = input.nextInt();This answers a question from lecture about ways of obtaining input that automatically convert the String entered by the user. Scanner has a variety of such options.
Q: If we only add the above code, what error do you get? Where is the actual code for the Scanner class? How can we import this code to use for our program?
After adding the code above, test it! It's good practice to test after every change. Then convert the remaining Python code into Java, thinking carefully about the type of each variable you declare. Test out your program with a variety of integers to make sure you get the results you were expecting.
Notes: you might want to use the builtin Math class for the square root function. Also, System.out.println takes only 1 argument, a String, which you can build up using the "+" operator.
/** * Tests whether an integer is prime * * @param n The number to test */ public static boolean prime(int n) { // TODO complete this method }
Finish implementing this method, using a for loop. To test whether one
number is divisible by another, you can use the mod operator, which in Java
is the symbol: %. After you finish implementing this method, add a
call to prime in main, the print the result. One
way to print the result is:
System.out.println(n + " is prime? " + isprime);
Part 2: Arrays
float[] numbers = new float[6];Create an array with space to hold ten int values. Then, using a loop, fill in the array as the multiples of n (number entered by the user), from 0 times n to 9 times n.
You can put this loop in main. There is actually an
existing function to print arrays, called
Arrays.toString. An example of how to use this code is
shown below:
System.out.println(Arrays.toString(multiples));
After you've finished the loop, test your code by printing the
multiples (add some text to the printed line to describe what you
are doing).
Copy over your code and log into aurora, for each operating system:
scp NumberFacts.java username@aurora.smith.edu:public_html/ ssh username@aurora.smith.edu
Below is the basic workflow once you are on aurora, with Java style
comments on the right. The
part in red will create a typescript.
cd public_html/ // change directory
mkdir lab1 // make new directory for Lab 1
mv NumberFacts.java lab1/ // move code into new folder
cd lab1 // move into Lab 1 folder
javac NumberFacts.java // compile code
java NumberFacts // test code
script // start recording the output
java NumberFacts // run your program as usual
exit // finish recording output
less typescript // view the script file, q to quit
rsubmit lab1 NumberFacts.java // submit the code
rsubmit lab1 typescript // submit the output
Make sure you've submitted these two files, then you're done! If you
finish early, you can start on Homework 1. If you are not finished
by the end of lab period, you have until midnight to submit.