June 1, 2023
You can find at https://spots.augusta.edu/caubert/teaching/general/java/prog.zip a simple java program, made of two files, Demo.java and Rectangle.java. You can also download them in a printable format.
To compile and execute them,
Install Java Development Kit (JDK). Hopefully the instructions at http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html should be clear enough. Using brew (for mac) or Chocolatey (for Windows) as package managers can make your installation easier.
Open a terminal and naviguate to the folder where you stored Demo.java and Rectangle.java.
To compile Rectangle.java using java compiler, type
javac Rectangle.java
in Linux/Unix-based systems, or (something like)
"C:\Program Files (x86)\Java\jdk1.8.0_151\bin\javac" Rectangle.java
in Windows. We will refer to those two commands as simply javac in the following: you can change the path and environment variables in Windows to make it more convenient to execute java compiler. A Rectangle.class file will be created, this program needs to be compiled before Demo.java, since this latter uses the class Rectangle. Once Rectangle is compiled, compile Demo.java, using
javac Demo.javaTo execute Demo.class, type
java DemoYou’ll be asked for your name and age. Just enter a string and hit the “enter” key, and then enter an integer and hit the “enter” key.
If you have questions, feel free to reach me at caubert@augusta.edu.
Those are really straightforward programs in java, hopefully allowing you to get familiar with this programming language. Start by reading Demo.java, the main purpose of those two files is to get some sense of the syntax, the overall construction will be very close to what you would do in any object-oriented programming-language.
Note that every java program
mainand that java is case sensitive, every statement ends with ;.
One “feature” of java that often confuses programmers is that you need to “flush” the buffer after using a nextInt (or next"AnythingThatIsn'tLine" actually) to be able to read again from it. You can find it explained e.g. at https://stackoverflow.com/a/13102066.
Another “feature” that can be confusing to C# programmers is that Java does not provide an equivalent to int.TryParse “natively”. How to emulate such feature is discussed e.g. at https://stackoverflow.com/q/1486077/.
You can start by installing an IDE for Java. Eclipse is popular, open-source, cross-platform and free, but quite heavy, choice, you may also want to consult e.g. this list on wikipedia to ease the comparison. Note that Visual Studio Code supports Java, and is probably already installed on your computer.
If you are interested in using Maven (a “a software project management and comprehension tool”), please refer to this archive. A good resource to install and configure Maven on a Windows computer is “How to Install Maven on Windows”. If you have the error message “Source option 5 is no longer supported. Use 6 or later”, then please follow the steps described here.
Playing with the code won’t hurt, and here are some suggestions of exercises:
int and a double, and store the result in a variable before displaying it at the screen again.String and iterate over it.String from the user and then display its first character at the screen.String containing a number into an int.Rectangle class.In CSCI 4711 - Software Engineering, Dr. Paul Attie uses Program Development in Java: Abstraction, Specification, and Object-Oriented Design, by Barbara Liskov and John Guttag. Addison-Wesley.