Lab_2_-_Basic_Programming_in_Java, Lab Reports of Object Oriented Programming

Lab_2_-_Basic_Programming_in_Java-BESE-10Lab_2_-_Basic_Programming_in_Java-BESE-10

Typology: Lab Reports

2019/2020

Uploaded on 05/13/2020

ahmad2246
ahmad2246 🇵🇰

5

(4)

7 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 212: Object Oriented Programming
Page 1
Department of Computing
CS 212: Object Oriented Programming
Class: BESE-10 AB
Lab02: Basic Programming in Java
Date: 29th January, 2020
Time: 9:00pm- 12:00pm & 2:00-5:00 pm
Instructor:
Ms. Hania Aslam
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Lab_2_-_Basic_Programming_in_Java and more Lab Reports Object Oriented Programming in PDF only on Docsity!

Department of Computing

CS 212: Object Oriented Programming

Class: BESE-10 AB

Lab02: Basic Programming in Java

Date: 29th^ January, 2020

Time: 9:00pm- 12:00pm & 2:00-5:00 pm

Instructor:

Ms. Hania Aslam

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?

  1. Naming your file incorrectly, then compiling.
  2. Forgetting a keyword such as void or class
  3. Forgetting a quotation mark "
  4. Forgetting a parenthesis ( or )
  5. Forgetting a dot.
  6. Using too many or too few braces { or }

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:

Write code to declare variables for s 0 , v 0 , a, and t , and then write the code to compute s on

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_.zip (without angle brackets) that contains ONLY the following files.

  1. All completed java source files representing the work accomplished for this lab: ActivityOne.java; ActivityTwo.java; ActivityThree.java; ActivityFour.java; ActivityFive.java; ActivitySix.java; ActivitySeven.java; ActivityEight.java; ActivityNine.java; Task1.java, Task2.java, Task3.java, Task4.java. The files should contain author in the comments at the top.
  2. A plain text file named README.TXT that includes a) author information at the beginning, b) a brief explanation of the lab, and c) any comments, or suggestions.

To Receive Credit

  1. By showing up on time for lab, working on the lab solution, and staying to the end of the class period, only then you can receive full credit for the lab assignment.
  2. Comment your program heavily. Intelligent comments and a clean, readable formatting of your code account for 20% of your grade.
  3. The lab time is not intended as free time for working on your programming/other assignments. Only if you have completely solved the lab assignment, including all challenges, and have had your work checked off for completeness by your TA/Lab Engineer should you begin the programming/other assignments.