





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Lab_2_-_Basic_Programming_in_Java-BESE-10Lab_2_-_Basic_Programming_in_Java-BESE-10
Typology: Lab Reports
1 / 9
This page cannot be seen from the preview
Don't miss anything!






Learning Objectives
After completing this lab you will be able to do variable declarations and initializations with some printing and math in Java.
We'll try a few experiments using it to see what happens for some basic errors. As you carry out the steps in this lab, it is important to think about what you are seeing and to understand what's going on. The more attentive you are here the fewer headaches you'll have later!
Basic Lab Instructions!
Talk to your classmates for help.
You may want to bring your textbook to future labs to look up syntax and examples.
Stuck? Confused? Have a question? Ask a TA/Lab Engineer for help, or look at the book or past lecture slides.
Complete as many problems as you can within the allotted time. You don't need to keep working on these exercises after you leave the lab.
Before you leave today, make sure to check in with one of the Lab Engineers/TAs in the lab to get credit for your work.
Activity #1.
Your first task will be to create a "Hello World" program using Java. Create a new directory CS-212, and navigate into it. Using notepad or the editor of your choice (preferably NetBeans), create a file called ActivityOne.java and enter the following code:
// This program outputs the message "Hello World" public class ActivityOne { public static void main (String args[]) { System.out.println ("Hello World"); } }
In case you are using NetBeans, just Build and Execute the project.
Alternatively, if you are working on a Linux like environment then save the file, and open the
Activity #3.
Programs should be indented properly to make them easier to read.
{ brace -> increase indent of following lines by one tab } brace -> decrease indent of that line and following lines by one tab
The following program has poor indentation. Fix it.
public class ActivityThree { public static void main(String[] args) { System.out.println("Properly indented programs"); System.out.println("look ever so much better!"); System.out.println("please fix me"); System.out.println("and make me beautiful again"); } }
Activity #4.
How many lines of output are produced (including blank lines)?
public class ActivityFour { public static void main(String[] args) { System.out.println("Testing, testing,"); System.out.println("one two three."); System.out.println();
System.out.println("How much output"); System.out.println(); System.out.println("will there be?"); } }
Activity #5.
The following program contains 11 errors! What are they?
public class ActivityFive public static main(String args) { System.out.println(Hello world); system.out.Pritnln("Do you like this program"?); System.out.println()
System.println("I wrote it myself."; { }
Once you think you've found the errors, create/compile/run a corrected version of this program.
Activity #6.
Discover what error messages the compiler produces when you make each of the following mistakes. How many unique error messages are you able to cause the compiler to produce?
Notice that the error messages don't always make it obvious what is wrong. But they usually tell you the right line number to fix.
Activity #7.
Write a complete Java program that produces the following output (note the blank line):
A "quoted" String is 'much' better if you learn the rules of "escape sequences."
Also, "" represents an empty String. Don't forget: use " instead of "! '' is not the same as ".
Task #1:
Writing a program that stores a student’s year (Freshman, Sophomore, Junior, or Senior), the number of courses the student is taking, and his or her GPA on a 4.0 scale. Declare variables with the appropriate names and types to hold this information. Print the stored information at the end.
Task #2:
Write an application that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print:
4 2 3 3 9
Assume that the user enters the correct number of digits. What happens when you execute the program and type a number with more than five digits? What happens when you execute the program and type a number with fewer than five digits?
Task #3:
Suppose you have a real number variable x. Write a Java expression that computes the following value y while using the * operator only four times:
y = 12.3x^4 − 9.1x^3 + 19.3x^2 − 4.6x + 34.
Task #4:
In physics, a common useful equation for finding the position s of a body in linear motion at a given time t, based on its initial position s0, initial velocity v0, and rate of acceleration a, is the following:
the basis of these values.
Hint: Java has a built-in function to compute power in Math class.
Hand in
Hand in the source code from this lab at the appropriate location on the LMS system. You should hand in a single compressed/archived file named Lab_1_
To Receive Credit