ic211. Inside that directory, create a
directory lab01 (you can nest it inside
a lab directory if you like!). Inside
the lab01 directory is where all the files you create
for this lab will reside.
| Ex0.cpp —The Simplest C++ Program | Ex0.java — The Simplest Java Program |
int main()
{
return 0;
} |
public class Ex0
{
public static void main(String [] args)
{
return;
}
} |
The source code for the simplest C++ programs consist of
.cppint main().The source code for the simplest Java program consists of
.javapublic static void main(String[] args).
← Note: member functions are
called methods in Java parlance.
main(String[] args) defined
in the class Something is actually
Something.main(String[] args), so that
main functions defined in different classes
technically have different names ... no conflict!
Already we see how the format of a Java program makes reusing
and combining code easier!
Note: It is not the case that
public static void main(String[] args)
has to be the only member function (or, as Java programmers
prefer, method) in the class definition.
You can add more function/method definitions in your
class just as you would add multiple function definitions in
your .cpp file while writing a C++ program.
javac)
to translate source code (.java files) into
lower-level code (.class files). It's also interpreted,
because .class files do not contain machine instructions to
be executed by the physical computer, but instead contain
instructions for the Java Virtual Machine (JVM), which is itself
just a program — i.e. the JVM is an interpreter for the
low-level language (called Java bytecode) contained in
the .class files.
Let's look at how to compile and run the Java version of everybody's friend: Hello World.
![]() |
![]() |
![]() |
Use an editor to create
the file HelloWorld.java
containing the source code
for the program. Java source code files are plain ol' text
files, just like C++ source code. No magic there!Note that the print statement System.out.println("foo");
is really not that different from cout << foo;,
because, although you've likely been blissfully unaware of
this, the long version of the C++ statement is
std::cout.operator<<("foo");
... which maps pretty nicely to the Java version.
|
Use a compiler (javac)
to translate the human-readable Java source code
into the class file
HelloWorld.class, which containins
Java bytecode, the language the JVM interprets.
|
Run the JVM (java) and have it execute
the code in the .class file. In particular,
it calls the function:
public static void main(String[] args)... from the class whose name matches the name of the file. In this csase, because the file is named HelloWorld.class, the JVM will execute the
main function defined in the
class HelloWorld. If no such class exists,
or no such function appears in the class, you get an
error.
|
Lab1a.java:
public class Lab1a
{
public static void main(String[] args)
{
}
}
Note: The name of the file must be exactly
Lab1a.java. Yes, upper-case vs. lower-case matters!
int[] x = new int[10];
int k = 0;
while(K < 10) {
x[k] = k*k;
k++;
}
for (int j = 0; j < 10;j++) {
System.out.println(j + " squared is " + x[j]);
}
What do you think it will do when run?
javac Lab1a.java(javac = "java compiler"). If you typed it in exactly as above, you should have gotten an error. Take a look at it, and notice how helpful javac is being. Fix the error.
lsyou'll notice a new file, Lab1a.class, which is the bytecode to be run by the java interpreter. Run the interpreter on your program with the command
java Lab1a
import java.util.*;at the very top of the file saying This is like
#include <iostream> in C++.
It allows us to use the Scanner class.
Scanner in = new Scanner(System.in);
System.out.print("Please input an integer ");
int n = in.nextInt();
System.out.print("Please input a second integer ");
int k = in.nextInt();
System.out.println("The two ints were " + n + " and " + k);
public static, otherwise they look
just like C++ functions.
Note that, unlike C++, there are no function prototypes separate from function
definitions. Test your function before continuing on.
Random rand = new Random(System.currentTimeMillis());After that point, every time we call
rand.nextInt(k), where k must be
an int, it will
return a random number between 0 (inclusive) and k
(exclusive). Random,
by the way, is in java.util, and has a lot more interesting functions
you can see in your book.
Write a program that generates a random number between 0 and 10 (inclusive!), and then asks the user for a guess until they get it correct. The more violent the berating on each wrong guess, the better. Code this in a file called Lab1c.java.