Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java Pre lab reading material: Read the following material before your lab session Textbook references: The String class Boolean expressions The if statement Before we

java

Prelab reading material: Read the following material before your lab session

Textbook references:

The String class

Boolean expressions

The if statement

Before we begin with the various flowcontrol statements in Java, lets have a quick review of some of the basic concepts already learnt. Ill cover only the core concepts here; refer the lecture notes and textbook for better explanation.

Refresher

Variables and Constants These are symbols that store values for you during the execution of your program. Picture them as boxes, with unique labels (name of the variable), restriction on what it can store (its data type) and some initial content (given to it during initialization). Constants are similar, only difference being that once initialized, their values cannot be changed.

Classes and objects Ill start with objects. Objects in java are akin to realworld objects. What are real world objects? Look at the desk you are sitting at. A pen, book, your computer etc. are all objects. Every object has two characteristics: state and behaviour. Even you, a human object, have these characteristics. You have a state (your name, address, age, gender etc) and behaviour (talking, walking, running, studying (the most important one) etc.). Similarly, Java objects have state and behaviour. The state of an object is described by the data it stores. Its behaviour is defined by the methods of that object.

There are numerous human objects around you. Everyone is different from everyone else, yet they are all human. This is saying that all humans are objects of the Class of humans (Anthropology anyone?). Basically, a class is the template from which different objects are created.

Expressions, statements and blocks An expression is made up of variables, operators and method calls. It evaluates to a single value. e.g., ++X + Y. It can be thought of to be the rough equivalent of a clause in English grammar. Multiple expressions together make a complete statement. A statement, hence, is similar to the English sentence. And just like a sentence in English has the full stop (the period .), it needs a terminating character, the semicolon (;). A collection of sentences make up a paragraph. In Java, a collection of statements make a block.

String class

This lab also requires reading the String class and the methods provided by it. You will read the textbook reference pointed out in the references. Make sure your basics about this class are clear before moving on to the second lab exercise. It requires you to use some of the more advanced methods in this class.

Now that the basics are clear, lets move on to controlflow statements, the theme for this weeks lab. Control flow statements allow you to control the flow, the execution of statements in your program depending on various conditions. Imagine driving West on University drive, toward ASU. Once you reach University and McAllister, you will take a left turn, not a right. Why? Because ASU is on the left, not right. You made a decision that altered the flow of your driving. Similarly, in Java, you evaluate a logical expression, and based on its result, you signal your program to execute a certain block of statements.

Logical Expressions

As covered before, expressions involve variables, values, operators and/or method calls. Logical expressions help you program make a decision. It involves the use of relational or logical operators.

Relational operators

These operators perform relational comparisons. They generate Boolean results on successful execution. The relational operators are binary, that is, they work on exactly two variables.

Operator

Operation

True

False

==

Equal to

5 == 5; a == a

5 == 7; a == b

!=

Not equal to

5 != 7; a != A

5 != 5; a != a

<

Less than

5 < 6; a < b

5 < 5; b < a

>

Greater than

5 > 4; b > a

5 > 5; a > b

<=

Less than or equal to

5 <= 5; 5 <= 6

5 <= 4

>=

Greater than or equal to

5 >= 5; 5 >= 4

5 >= 6

Logical operators

Logical operators operate on Boolean variables an evaluate them to produce Boolean results. Recall that Boolean variables can either be true or false. The operators are:

1) ! (logical NOT) inverts the Boolean value; i.e. evaluates to true if its variable is false, evaluates to false if the variable is true

a. !true = false, !false = true

2) && (logical AND) evaluates to true if and only if both variables are true, else returns a false

true && true = true

true && false; false && true = false

false && false intuitively is false

3) || (logical OR) evaluates to true if at least one of the variable is true

true || true = true

true || false; false || true = true

false || false is still false

Note that the logical operators operate on Boolean values. And as mentioned previously, relational operator produce Boolean values as their results. This allows us to club them to make pretty complex expressions: (relational expression1) (relational expression2) ... (relational expressionN)

e.g., (5>4) || (variable1 != variable2)

So, now we know how to use logical expressions. And we know that they will evaluate to one of two possible values in the end: either true or false. We will use this property of logical expressions to introduce some control over the execution of statements within our program.

The if statement

The if statement is a unary selection, oneway selection statement. Do something, if this condition holds. You typically use if statements in cases when you want to do something extra, in cases like special input, some predetermined outcomes etc.

The ifelse statement

This is a twoway or binary selection statement. It lets you perform actions if a certain condition holds and also if it doesnt.

We will be using logical expressions discussed previously to make the if and ifelse statements and consequently the program follow our wishes during execution. Now that you are comfortable with the concepts of logical expressions and selection/decision statements, lets code!

Lab exercise: Complete the following exercises in your lab session

Do not forget to ask your TA for any help you need.

1. Control statements: Lab4.java

Go ahead and download the file Lab4.java from blackboard to your working folder on your computer. Complete this program to determine the raise and new salary for an employee by adding if ... else statements to compute the raise.

The input to the program includes the current annual salary for the employee and a number indicating the performance rating (1=excellent, 2=good, and 3=poor).

Compute the raise as follows: An employee with a rating of 1 will receive a 6% raise, an employee with a rating of 2 will receive a 4% raise, and one with a rating of 3 will receive a 1.5% raise.

Submission Instructions:

Name your program for the question 1 as follows

Lab4.java.

And this file needs to be submitted using the Submit Labs/Assignments button in the blackboard site. (In the submission site, you need to login using the account that you have created before, and choose Lab Submission and Lab4) Make sure that your file has your name, lab letter, and a description of your program in comments.

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

Database Security XI Status And Prospects

Authors: T.Y. Lin, Shelly Qian

1st Edition

0412820900, 978-0412820908

More Books

Students also viewed these Databases questions

Question

Explain in detail the developing and developed economy of India

Answered: 1 week ago

Question

Problem: Evaluate the integral: I = X 52+7 - 1)(x+2) dx

Answered: 1 week ago

Question

What is gravity?

Answered: 1 week ago

Question

What is the Big Bang Theory?

Answered: 1 week ago