KEMBAR78
Homework 0 | PDF | Java (Programming Language) | Java Virtual Machine
0% found this document useful (0 votes)
18 views3 pages

Homework 0

Uploaded by

Chaewon Min
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Homework 0

Uploaded by

Chaewon Min
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Homework 0

Java Basics

Hello, and welcome to your first homework assignment! Don't worry, though - you won't have to
turn this in. This homework just serves to make sure you have installed Java properly on your
machine and that you know how to run a Java program.

This homework comes to you in 3 parts!

Part 1: Installing Java


In this class we will be using Java 8, so be sure you get Java 8 installed, even if you have a
previous version of Java. We have detailed instructions for this on our class website:
http://www.cc.gatech.edu/classes/AY2017/cs1331a_spring/started/index.html

Check to see which version of Java you have installed by running “javac -version” and “java
-version” .

After running “javac -version” , you should see this output, where X_YY is your version of the
JDK:
javac 1.8.X_YY

After running “java -version” , you should see this output, where X_YY is your version of the
JDK:
java version "1.8.X_YY"
Java(TM) SE Runtime Environment (build 1.8.0_66-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b17, mixed mode)

Part 2: Writing and compiling a Java class


In order to create a Java class, you must first create a file with a .java extension. In our example
here, we will use Test.java . Within the class, you will need to write a class header. For now, just
memorize that you need to write "public class" before your class name. We will go more in depth
about what exactly this means later. As for the class name, it must exactly match the name of the
file.

Thus, our example should look like this:

public class Test {

Now that you have written a Java class, you should be able to compile your code! Navigate to
the directory where you have saved this file in the command line.

After navigating to the proper directory, you will be able to compile the Java class you've just
created! You do this by using the command "javac Filename.java" .
Since we named our Java example file "Test.java" , we compile it using "javac Test.java" .

This should create a new file in your directory called "Test.class" ! Created by the Java compiler,
this file will contain Java bytecode, and it will not make much sense to you if you open it up.
However, it makes a lot of sense to the Java Virtual Machine, or the JVM (a computing machine
that allows our program to run).

If a new file was not created, your program did not compile. Read the compilation error
produced in your command prompt, and fix it before proceeding!

Part 3: The main method


Remember, the method signature should look like this:

public static void main (String[] args) {

This method is critical for a Java class to be "runnable."

Write a main method for your Test class. All of the rest of the code will be written inside of your
main method!
Now, you can write the contents of your first Java program! Simply, try these two steps:
a. Print out "Hello, world!"

b. Declare three variables:


An integer x
An integer y
An integer k

Assign a value of 8 to the variable x.


Assign a value of 5 to the variable y.
Assign x * y + 2 to the variable k.

Print out the value of the variable k.

Try to compile as you make changes. This will help you correct any errors you may make as you
go instead of dealing with them all at the end.

To run your program, use the command “java Filename” . Since we named our file Test.java, we
will use the command “java Test” . If you ever want to run a new version of your program after
making alterations, know that you must recompile before doing so.

Good luck, and happy hacking!

Summary:
If you had trouble with any parts of this homework, stop by office hours, starting this
Thursday. We would love to help you figure out the basics before jumping into Homework 1. All
teaching assistants’ office hours can be found on T-square under the Office Hours tab.

You might also like