Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1.17 Lab1a: HelloWorld This is an introduction to the Java language and debugging. We will begin by creating the traditional HelloWorld program. ZyBooks has its

1.17 Lab1a: HelloWorld

This is an introduction to the Java language and debugging. We will begin by creating the traditional HelloWorld program.

ZyBooks has its own integrated text editor so we will be using that for the foreseeable future for convenience, though it is advisable to get yourself acquainted with an Integrated Development Environment (IDE) or a text editor of your choice.

HelloWorld Program

Begin by typing:

public class HelloWorld 

Take note that the name of the program file must match the name of the class. In this case, the program file is called HelloWorld.java (in ZyBooks the name of the program file is located at the top of the editing window) and the class is HelloWorld.

Also note that Java is case sensitive so helloworld is different from HelloWorld.

Now, open a curly brace (this thing { ) and add a few empty lines then add a closing brace (this other thing } ).

The curly braces denote everything that belongs to the Hello class. Right now it is empty but we will be adding the main()method to it. The main() method is the entry point to the program and is where it will start when it is run.

Within the Hello class, within the space denoted by the curly braces, type:

public static void main(String[] args) 

This is the header for the main() method, the components that make this will be explained further in the future. For those curious now, you can find a bit more information here.

Give this method some curly braces, and now we have an empty main() method.

Inside the main() method, within the curly braces corresponding to main(), type:

System.out.println("Hello, world!"); 

ZyBooks has two options beneath the editing window: develop and submit.

The develop option allows you to compile and run your code with input of your choice. Feel free to use this to experiment with this option to see outputs and test your code.

The submit option will be how your code will be graded. It will be compiled and run using predefined input and expected output. The output comparisons are fairly strict, so apologies in advance for tedious formatting corrections you will have to do. You can submit as many times as necessary in order to get full credit on these labs.

Note that in ZyBooks, the run button both compiles and runs the code, similar to running a program on an IDE. If you were to do this through the command line, you would use the javac and java commands.

Hit submit and that concludes your HelloWorld program!

Introduction to Debugging Compile Time Errors

Now that you have successfully created and submitted your first program, it's time for some debugging practice.

This portion cannot be graded and will not be submitted (do this in the develop section of ZyBooks). It is purely for your benefit and it is up to you how much you get out of it.

Inside of main(), add the line:

System.out.println("we are debugging) 

Then compile and run. It will produce an error message along the lines of:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: String literal is not properly closed by a double-quote at Lab1a.main(Lab1a.java:6) 

Error messages are usually pretty straight forward, they simply take some practice reading.

The very last line:

at HelloWorld.main(HelloWorld.java:6) 

tells where the error is located. In this case, it says in the HelloWorld.java file on line 6. There is further information that can be gleaned like it is in the class HelloWorld, specifically the method called main.

The type of error is found in the first few lines:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: String literal is not properly closed by a double-quote 

The first line is the technical error, and you can look that up for further information (Google is your friend). The second line gives a more human understandable explanation of why the error was thrown. In this case it states the String was not closed by a double quote.

So with this information, we go to line 6 (or whatever line was specified) of HelloWorld:

System.out.println("we are debugging) 

and we see that indeed our String (we are debugging) was not closed. So we add a double-quote after debugging, closing the String.

If we run it again, that error should be gone but in its place is another error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error, insert ";" to complete Statement at Lab1a.main(Lab1a.java:6) 

Using the same process, this error can also be resolved fairly quickly. It would be a good idea to get familiar with reading these types of error so spend so time experimenting.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions