Starting from:

$25

CSE110 - Principles of Programming with Java - LAB 2  - Solved

What this Lab Is About:  

•          This program is for practicing the use of primitive data types, expressions. – Chapter 2  

 

Use the following Coding Guidelines:  

•          When declaring a variable, you usually want to initialize it.  

•          Remember you cannot initialize a number with a string.  

•          Remember variable names are case sensitive.  

•          Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.  

•          Use white space to make your program more readable.  

•          Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs.  

 

At the beginning of each programming assignment you must have a comment block with the following information. The number in the left column indicates the line number. Go to the View menu à Line Numbers in TextPad, then you will see the line numbers, which is very helpful in fixing the errors.

 

  

 

1.      Getting Started  
 

Create a class called Lab2. Use the same setup for setting up your class and main method as you did for the previous assignments. Be sure to name your file Lab2.java.  

Hints: See the sample output below in the lab for an idea of what your program should output.

 

2.      Programming exercises
 

The following is a template of Lab2.java. It has three parts. Finish the part1, 2 and 3 one by one. Once you finish all parts and run the program correctly in your PC, submit the Lab2.java to the online site. The template Java code has each instruction as a comment block, but you don't have to type them in your code.  

 

/****************************************************** Part 1: Declaring Variables: 

Read the following code skeleton and add your own code according to the comments.  

Note:When you see //--> that is where you need to add Java code.  

********************************************************/

  

 

/******************************************************

Part 2: Assign Values to Variables  
After part 1, write a segment of code which will assign values to  the variables and print out the following:

********************************************************/

 

  

 

        /******************************************************

       Part 3: Using Mathematical Operators  
       After part 2, we will be finding the average of all the 4 numbers n1,n2,n3,n4 and         will be initializing the variable avg with this result.

       ********************************************************/

  

 

3.      Check the smaple output  
Below is an example of what your output should look like when you submit the file to the online submission site.  

Note: There are several approaches to solve a programming question. Make sure your output is reasonable. The goal here is for you to demonstrate that you understand the underlying concepts.  

 

Sample Run:  

Pratik is 22 years old. 

Pratik Iyer 

Input integer value for n1,n2,n3,n4 









 The Value of n1 is:1  

 The Value of n2 is:2  

 The Value of n3 is:3  

 The Value of n4 is:4 

The average of the four numbers is:2.5 


More products