Lab report object oriented programming, Study Guides, Projects, Research of Cooperative Governance

Object-oriented programming (OOP) refers to a type of computer programming (software design) in which programmers define the data type of a data structure, and also the types of operations (functions) that can be applied to the data structure.

Typology: Study Guides, Projects, Research

2019/2020

Uploaded on 01/11/2020

samia-khan-2
samia-khan-2 🇵🇸

4.8

(4)

11 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
LAB 3
TOPICS:
Reading input using the Scanner class
Methods of the String class
The if statement
Methods that accept parameters and return
values
Exercise 1:
In BlueJ, create a new project called Lab3
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Lab report object oriented programming and more Study Guides, Projects, Research Cooperative Governance in PDF only on Docsity!

LAB 3

TOPICS:

  • Reading input using the Scanner class
  • Methods of the String class
  • The if statement
  • Methods that accept parameters and return

values

Exercise 1:

  • In BlueJ, create a new project called Lab

Figure 1

  • Create a class in that project called LotsOfStuff Figure 2

Exercise 2:

  • Prompt the user to enter their first name. Use a print statement (instead of println) for the prompt so the user input will appear on the same line.
  • Read the input using a call to nextLine of the Scanner object and store the value in a String variable called name
  • Print a greeting as shown below (user input is shown in blue): Enter your first name: Bob Nice to meet you, Bob!
  • Don't forget the exclamation point in the greeting. SOLUTION:
  • Figure

Figure 5 OUTPUT: Figure 6

Exercise 4:

  • Add a second if statement so that, if the name entered is "Bob", another line of output is produced: I've always liked you, Bob.
  • Otherwise, print the following two lines using separate println statements: You're not Bob! But I like you anyway. SOLUTION: Figure 7 OUTPUT:
  1. IF THE INPUT IS BOB: Figure 8

SOLUTION: Figure 10 OUTPUT: Figure 11 Exercise 6:

  • Modify the printPerimeter method so that it only computes and prints the perimeter if the input value is greater than 0.
  • If it's not, print: The length must be positive. SOLUTION:

Figure 12 OUTPUT:

  1. IF THE OUTPUT IS POSITIVE: Figure 13
  2. IF THE OUTPUT IS NEGATIVE: Figure 14

Exercise 8: Instead of running the method directly through BlueJ, let's write one method that calls another. Write a method named caller (static, no parameters, no return value). In caller, call the computePerimeter method, passing it a value of 10, and print the result. Then call the computePerimeter method again from caller, passing it a value of 15, and print the result. Compile and run the caller method.

SOLUTION: Figure 16 OUTPUT: Figure 17