To set up a virtual machine (VM) on Google cloud, with Java installed. This will allow you to have a command-line machine for doing the labs and projects for this course.
The instructions for setting up your Google Cloud account, and creating a virtual machine, are on the CPSC 225 page. You can find the instructions here:
Follow the linked instructions through the "Connecting with a Client" section.
You should now have a Google Cloud VM up and running. The next step is to install the Java software development kit. In order to do this, you will need to run the following command:
$ sudo apt install openjdk-11-jdk-headless
Answer 'Y' when it asks you if you want to continue. This will take a short while to complete. When it's done, you should have Java up and running. In order to test it, run the "javac -version" and "java -version" commands. If everything went well, you should see this:
$ javac -version javac 11.0.5 $ java -version openjdk version "11.0.5" 2019-10-15 OpenJDK Runtime Environment (build 11.0.5+10-post-Ubuntu-0ubuntu1.118.04) OpenJDK 64-Bit Server VM (build 11.0.5+10-post-Ubuntu-0ubuntu1.118.04, mixed mode, sharing)
If you see something like this, then you should be good to go!
To get warmed up to programming in this environment, write a simple program to read in numbers from the user, and print the average to the screen. Your program will need to create a Scanner for reading the input, as well as a loop to read in all of the data.
An example run is shown below:
$ java Average Enter number of elements: 4 Enter value: 3 Enter value: 12 Enter value: 27 Enter value: 5 Average is 11.75
To compile a Java program, we use the command
javac followed by
the name of the source code file. For example, if there is a program called
"Hello.java", then the following command would compile it:
$ javac Hello.java
This only compiles the program. The compiler translates your Java
source code into bytecode. It doesn't actually run your program. You
can use the
ls command to see that this command makes a file called
$ ls Hello.java Hello.class
In order to run the program, we'll need to use the
command, followed by the name of main class of the program. For programs with
just one file, that means the name of the file with no ".java" at the end. For
the example above, it would look like this:
$ java Hello Hello World!
This will take the compiled .class file, and actually execute it, doing whatever the program is supposed to do.
Use these commands to compile and test your average program.
When you are finished, please email me your program at firstname.lastname@example.org. To do this, you will need to get the files off of your VM to email. You can do this following these instructions.
Copyright © 2019 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.