CSE 1325 – Fall 2015

Object-Oriented and Event-driven Programming in Java

Lab 2

 

Assigned:        Tuesday, September 8, 2015

Due:                Friday 5:00 pm Sept. 11, 2015

The Lab assignment will be graded out of 100 points.  There are multiple parts or tasks that make up each Lab.

Some tasks ask you to write code, and specify what name to use for the file where you save that code. You need to use exactly the name that is given (do not change the case, or make any other modification). Remember, the name of the main class must match the filename.

For some tasks you need to answer questions. Create a text document entitled answers.txt, or lab2answers.docx, or lab2answers.pdf, and put all your answers there. Acceptable file formats are plain text, Word document, OpenOffice document, and PDF. Put your last name, first name and UTA ID in the file on the first line.  Label the answers for each question with the number/letter of the question.

Each task below will instruct you where to put your answers.

Grading Rubric for Lab 2:

 

Part 1:  15 points

 

Part 2:  20 points

 

Part 3:  20 points

 

Part 4:  20 points

 

Part 5:  25 points

 

Miscellaneous:  If you have questions, e-mail Dr. T (tiernan@uta.edu) and/or the TAs (see website for contact info)

 


 

Lab 2 parts to complete:

 

Part 1:  Create a new Java application Lab2Part1 and then enter the code below.

 

public class Lab2Part1

{

            public static void main(String[] args)

            {

                        int num1 = 9;

                        int num2;

double val1 = 9.0;

double val2;

num2 = 3;

val2 = 6;

 

System.out.println(ÒPrinting valuesÓ);

                        System.out.println(Ònum1 value = Ò+num1);

                        System.out.println(Ònum2 value = Ò+num2);

                        System.out.println(Òval1 value = Ò+val1);

                        System.out.println(Òval2 value = Ò+val2);

 

                        num2 = num2 / num1;

                        System.out.println( Ònum2/num1 = num2 = Ò+num2 );

                        num1 = 3;

                        val2 = num1 / val1;

                        System.out.println( Ònum1 = 3: num1/val1 = val2 = Ò+val2 );              

 

                        val2++;

                        val1 += val2;

                        System.out.println(Òafter val2++: val2 value = Ò+val2);

                        System.out.println(Òafter val1 += val2: val1 value = Ò+val1);

 

                        System.out.println(val1 > num1);

                        System.out.println( val2 != num2 );

            }

}

 

1.a)  If you execute this program, what will be printed in the output window?  (5 points)

 

1.b)  The operator += is an abbreviation for what actions?  Write this in words.  (5 pts)

 

1.c)  Rewrite the statement val1 += val2; to use only assignment and addition operators.  (5 pts)

 

Put your answers for 1.a, 1.b, and 1.c in your answers file.

Part 2:  The code below does not work.  Create a new Java program that modifies this so that it correctly calculates the distance between two points.   Note that the import statement needs to be before the class header Òpublic classÉÓ but after a package header (if there is one.)

 

import java.lang.Math;

public class Part2

{

            public static void main(String[] args)

            {

                      double x1 = 4; 

                      double y1 = 1;

                      double x2 = 1;

                      double y2 = 5; 

                    int xDist = x1 – x2;  // do NOT change the data type of xDist

                    int yDist = y1 – y2;  // do NOT change the data type of yDist

                      double distance;       

                      distance = Math.sqrt( pow(xDist,2) + pow(yDist,2) );       

 

                      //  the square root of (x1 - x2) squared plus (y1 - y2) squared

 

                     System.out.printf("The x distance is %d and the y distance is %3d \n", xDist, yDist);

                      System.out.print(ÒThe distance between (Ò+x1+Ó,Ó+y1+Ó) and Ò); 

                      System.out.printf("(%.0f + %3.0f) is %4.1f \n", x2, y2, distance);

            }

}

 

2.a)  List each different type of error, giving the NetBeans error message, that you find after typing this program into Netbeans exactly as given. Put these in your answer file.  (4 pts)

 

2.b)  Also, in your answers file, explain exactly (only a few words are needed) why the code, as given above, does not work, i.e. why did each error occur?  (4 pts)

 

2.c)  Now correct all the errors and run the program, debugging until it works properly. Only three lines of the program need to be modified to remove the compiling errors.  Be sure to follow any restrictions specified in the program comments.  None of the output statements should be changed.  Once the program works, save it as Lab2Part2.java .   (8 pts)

 

2.d)  In your answers file, show the EXACT output that is produced by the program. (4 pts)

 


 

Part 3:  Given the code below, create a new Java application.

 

public class Lab2Part3

{

    public static void main(String[] args)             // String thing test function

    { 

       /* *****

Declare the needed input object here using the names shown below in the program.  

Also add any other needed lines to make the input object function.

       ****** */

 

       //When running the program, first input should be "Carter" entered without the quote marks

        System.out.print("Please enter a first name: ");

        String first = input.next();

 

        //input here should be "Tiernan"

        System.out.print("Please enter a last name: ");

        String last = input.next();

 

        String firstInit = "J.";

        char middleInit = 'M';

        char dot = '.';

        String title = "Dr.";

        String sp = " ";

        String fullName;  // should be title, firstInit, first name, then middleInit, then last name

       

        fullName = title+sp+ firstInit+sp+first+sp+middleInit+dot+sp+last;

        System.out.println("The full name is "+fullName);

    }

}

 

3.a)  Add the needed code to the program above to make it run.  Save the program as Lab2Part3a.java .  (5 pts)

 

3.b)  In the answers file, show exactly the output produced by the program if all instructions in the program are followed.   (4 pts)

 

3.c)  What is different about variables middleInit and dot from the other variables in the program and why can they be used with the other variables?  Put your answer in the answers file.  (4 pts)

 

3.d)  Rewrite the program so that it accepts the same inputs and produces the same output but variable fullName is removed from the program.  Do not create a new variable to replace fullName.  Save this modified program as Lab2Part3d.java .   (7 pts)

 


 

Part 4:  Given the information below, write a Java program to implement this algorithm. 

 

Define a variable called ÒparcelHeightInInchesÓ and give it the value 12.5 .

Define a variable called ÒparcelWidthInInchesÓ and give it the value 10.25 .

Define a variable called ÒparcelWeightInOuncesÓ and give it the value 24.

 

Using a built-in Math function, increase the parcelHeight to the next higher whole number value.  [Hint: look at round, floor, and ceiling functions.]

Using a built-in Math function change the parcelWidth to the closest whole number.

 

Calculate the area of the parcel using the modified height and width values.

Calculate the weight per square inch of the parcel.

 

Print out the final values for the height, width, total weight, area, and weight per square inch.  Make sure to label the output values for the user.

 

4)  Save your working program as Lab2Part4.java .  (20 pts)

 

 

 

 

 

Part 5:  Given the program you created for Part 4, modify that program in the following ways.

 

      Accept user input values for height, width and weight then do the same modifications and calculations.

 

      Produce output that has the same values and titles and is spaced EXACTLY like the output below:   [Your font does NOT have to be the same.]  The first title is in a 10 space length then has two spaces separating it from the next title.  The last title is larger.

     

    Height       Width      Weight        Area    Wt per Sq In

     11.00       14.00       88.90      154.00          0.58

 

5.a)  Save your working program as Lab2Part5.java .  (13 pts)

 

5.b)  Give two different sets of input values that will produce the output shown above where the height and width are not the same values as shown above.  This means that the height cannot be 11.0 and the width cannot be 14.00.  Put your two sets of input values in your answers file.  Write one set of three values on one line then write the second set of three values (using a different height and width) on the next line in the answers file.  (12 pts)

 

 

 


 

--------------------------------------------------------------------------------------------------------

Suggestions

Pay close attention to all requirements on this page, including file names and submission format. Even in cases where the program works correctly, points will be taken off for not following the instructions given on this page (such as wrong file names, wrong compression format for the submitted code, and so on). The reason is that non-compliance with the instructions makes the grading process significantly (and unnecessarily) more time consuming. Contact the instructor or TA if you have any questions.

 

How to submit

The assignment should be submitted via Blackboard. Submit a ZIPPED directory called lab2LastnameI.zip where ÒLastnameIÓ should be replaced with your last name followed by your first initial, ex. my filename would be lab2TiernanC.zip .  The file must be zipped. No other forms of compression accepted. (Contact the instructor or TA if you do not know how to produce .zip files). The zipped directory should contain your answers document and all the Java code files (task3.java, etc).

 

To create a zipped directory called lab2LastnameI.zip, follow these steps:

1.         Create a folder called lab2LastnameI.

2.         Copy to that folder all your solutions (your answers file, and all your Java files).

3.         Zip that folder. On Windows, you can zip a folder by right-clicking on the folder, and then selecting Send to->Compressed (zipped) folder.   On Mac, go to the parent directory of your lab2 directory in a Finder window, click on your lab2LastnameI folder, then select Compress from the list of file actions (under the gear icon).

 

Submission checklist

¥          Did you answer all of the questions in the lab assignment and did you create all the required .java program files?

¥          Did you create the answers file with your name, UTA ID, and answers to non-programming tasks?

¥          Did you zip everything into a file called lab2LastnameI.zip?

¥          Did you upload the zipped file to Blackboard before the due date and time?