Chapter 5 Lab Methods

Similar documents
Chapter 5 Lab Methods

Chapter 5 Lab Methods

for Diane Christie University of Wisconsin Stout

Chapter 6 Lab Classes and Objects

Perimeter, Area, Surface Area, & Volume

Chapter 3 Lab Decision Structures

Task #1 The if Statement, Comparing Strings, and Flags

AP Computer Science Unit 1. Writing Programs Using BlueJ

Area of Polygons And Circles

Basic Computation. Chapter 2

AP Computer Science Unit 1. Writing Programs Using BlueJ

Chapter 1 Lab Algorithms, Errors, and Testing

Introduction to Java Unit 1. Using BlueJ to Write Programs

Chapter 9 Lab Text Processing and Wrapper Classes

Introduction to Java Applications

Surface Area and Volume

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Area rectangles & parallelograms

1 Short Answer (10 Points Each)

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Area and Perimeter Name: Date:

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

BASIC INPUT/OUTPUT. Fundamentals of Computer Science

A+ Computer Science -

Input. Scanner keyboard = new Scanner(System.in); String name;

For personnal use only

Chapter 6 Lab Classes and Objects

Question 2. [5 points] Given the following symbolic constant definition

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

CIRCLES ON TAKS NAME CLASS PD DUE

Object-Based Programming. Programming with Objects

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

2D Geometry Part 2: Area

Evaluating the Style of your programs

Anatomy of a Java Program: Comments

A+ Computer Science -

Chapters 1.18 and 2.18 Areas, Perimeters and Volumes

Repetition CSC 121 Fall 2014 Howard Rosenthal

2D Geometry Part 2: Area

CS2 Assignment A1S The Simple Shapes Package

COMP 202 Java in one week

CSIS 10A Assignment 3 Due: 2/21 at midnight

1 Short Answer (10 Points Each)

Someone else might choose to describe the closet by determining how many square tiles it would take to cover the floor. 6 ft.

Advanced Computer Programming

SPRINGBOARD UNIT 5 GEOMETRY

Properties of a Circle Diagram Source:

2. What are the two main components to the CPU and what do each of them do? 3. What is the difference between a compiler and an interpreter?

BlueJ Demo. Topic 1: Basic Java. 1. Sequencing. Features of Structured Programming Languages

Lesson 7 Part 2 Flags

Practice Midterm 1. Problem Points Score TOTAL 50

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Computational Expression

Geometry. Geometry is the study of shapes and sizes. The next few pages will review some basic geometry facts. Enjoy the short lesson on geometry.

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

Name: Class: Date: 2. I have four vertices. I have four right angles and all my sides are the same length.

Basic Computation. Chapter 2

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Lab 9: Creating a Reusable Class

COMP 202. Java in one week

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

S8.6 Volume. Section 1. Surface area of cuboids: Q1. Work out the surface area of each cuboid shown below:

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

CSCI 261 Computer Science II

Data Types and the while Statement

Chapter 12: Arrays Think Java: How to Think Like a Computer Scientist by Allen B. Downey

Top-Down Program Development

Figuring Areas. Instructions. Name Date

Free Response. Test A. 1. What is the estimated area of the figure?

MACS 261J Final Exam. Question: Total Points: Score:

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Grades 7 & 8, Math Circles 20/21/22 February, D Geometry Solutions

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Geometry 10 and 11 Notes

Geometry Surface Area and Volume of Pyramids and Cones.

4. Java language basics: Function. Minhaeng Lee

Geometry - Additional Questions: Sheet 1

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

CS 177 Recitation. Week 8 Methods

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

Number. Number. Number. Number

1.4 Perimeter, Area and Surface Area of Similar Figures

Midterm Examination (MTA)

DO NOW Geometry Regents Lomac Date. due. 3D: Area, Dissection, and Cavalieri

CSCE 145 Exam 1 Review Answers. This exam totals to 100 points. Follow the instructions. Good luck!

Unit 4 End-of-Unit Assessment Study Guide

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Example Program. public class ComputeArea {

Grades 7 & 8, Math Circles 20/21/22 February, D Geometry

Math 6 Unit 9 Notes: Measurement and Geometry, Area/Volume

AP Computer Science Unit 1. Programs

Over and Over Again GEEN163

Chapter 3. Ch 1 Introduction to Computers and Java. Selections

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Transcription:

Gaddis_516907_Java 4/10/07 2:10 PM Page 41 Chapter 5 Lab Methods Objectives Be able to write methods Be able to call methods Be able to write javadoc comments Be able to create HTML documentation for our Java class using javadoc Introduction Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order. This also allows for efficiencies, since the method can be called as many times as needed without rewriting the code each time. Finally, we will use documentation comments for each method, and generate HTML documents similar to the Java APIs that we have seen.

Gaddis_516907_Java 4/10/07 2:10 PM Page 42 42 Lab Manual to Accompany Starting Out with Java 5: From Control Structures to Objects Task #1 void Methods 1. Copy the file Geometry.java (code listing 5.1) from www.aw.com/cssupport or as directed by your instructor. This program will compile, but when you run it, it doesn t appear to do anything except wait. That is because it is waiting for user input, but the user doesn t have the menu to choose from yet. We will need to create this. 2. Above the main method, but in the Geometry class, create a static method called printmenu that has no parameter list and does not return a value. It will simply print out instructions for the user with a menu of options for the user to choose from. The menu should appear to the user as: This is a geometry calculator Choose what you would like to calculate 1. Find the area of a circle 2. Find the area of a rectangle 3. Find the area of a triangle 4. Find the circumference of a circle 5. Find the perimeter of a rectangle 6. Find the perimeter of a triangle Enter the number of your choice: 3. Add a line in the main method that calls the printmenu method as indicated by the comments. 4. Compile, debug, and run. You should be able to choose any option, but you will always get 0 for the answer. We will fix this in the next task.

Gaddis_516907_Java 4/10/07 2:10 PM Page 43 Chapter 5 Lab Methods 43 Task #2 Value-Returning Methods 1. Write a static method called circlearea that takes in the radius of the circle and returns the area using the formula A = pr 2. 2. Write a static method called rectanglearea that takes in the length and width of the rectangle and returns the area using the formula A = lw. 3. Write a static method called trianglearea that takes in the base and height of the triangle and returns the area using the formula A = 1 2 bh. 4. Write a static method called circlecircumference that takes in the radius of the circle and returns the circumference using the formula C = 2pr. 5. Write a static method called rectangleperimeter that takes in the length and the width of the rectangle and returns the perimeter of the rectangle using the formula P = 2l + 2w. 6. Write a static method called triangleperimeter that takes in the lengths of the three sides of the triangle and returns the perimeter of the triangle which is calculated by adding up the three sides.

Gaddis_516907_Java 4/10/07 2:10 PM Page 44 44 Lab Manual to Accompany Starting Out with Java 5: From Control Structures to Objects Task #3 Calling Methods 1. Add lines in the main method in the GeometryDemo class which will call these methods. The comments indicate where to place the method calls. 2. Below, write some sample data and hand calculated results for you to test all 6 menu items. 3. Compile, debug, and run. Test out the program using your sample data.

Gaddis_516907_Java 4/10/07 2:10 PM Page 45 Chapter 5 Lab Methods 45 Task #4 Java Documentation 1. Write javadoc comments for each of the 7 static methods that you just wrote. They should include a) A one line summary of what the method does. a) A description of what the program requires to operate and what the result of that operation is. a) @param listing and describing each of the parameters in the parameter list (if any). a) @return describing the information that is returned to the calling statement (if any). 2. Generate the documentation. Check the method summary and the method details to ensure your comments were put into the Java Documentation correctly.

Gaddis_516907_Java 4/10/07 2:10 PM Page 46 46 Lab Manual to Accompany Starting Out with Java 5: From Control Structures to Objects Code Listing 5.1 (Geometry.java) import java.util.scanner; /** This program demonstrates static methods */ public class Geometry { public static void main (String [] args) { int choice; //the user s choice double value = 0; //the value returned from the method char letter; //the Y or N from the user s decision //to exit double radius; //the radius of the circle double length; //the length of the rectangle double width; //the width of the rectangle double height; //the height of the triangle double base; //the base of the triangle double side1; //the first side of the triangle double side2; //the second side of the triangle double side3; //the third side of the triangle //create a scanner object to read from the keyboard Scanner keyboard = new Scanner (System.in); //do loop was chose to allow the menu to be displayed //first do { //call the printmenu method choice = keyboard.nextint(); switch (choice) { Code Listing 5.1 continued on next page.

Gaddis_516907_Java 4/10/07 2:10 PM Page 47 Chapter 5 Lab Methods 47 case 1: "Enter the radius of the circle: "); radius = keyboard.nextdouble(); //call the circlearea method and //store the result //in the value "The area of the circle is " + value); case 2: "Enter the length of the rectangle: "); length = keyboard.nextdouble(); "Enter the width of the rectangle: "); width = keyboard.nextdouble(); //call the rectanglearea method and store //the result in the value "The area of the rectangle is " + value); case 3: "Enter the height of the triangle: "); height = keyboard.nextdouble(); "Enter the base of the triangle: "); base = keyboard.nextdouble(); //call the trianglearea method and store //the result in the value "The area of the triangle is " + value); Code Listing 5.1 continued on next page.

Gaddis_516907_Java 4/10/07 2:10 PM Page 48 48 Lab Manual to Accompany Starting Out with Java 5: From Control Structures to Objects case 4: "Enter the radius of the circle: "); radius = keyboard.nextdouble(); //call the circumference method and //store the result in the value "The circumference of the circle is " + value); case 5: "Enter the length of the rectangle: "); length = keyboard.nextdouble(); "Enter the width of the rectangle: "); width = keyboard.nextdouble(); //call the perimeter method and store the result //in the value "The perimeter of the rectangle is " + value); case 6: "Enter the length of side 1 " + "of the triangle: "); side1 = keyboard.nextdouble(); "Enter the length of side 2 " + "of the triangle: "); side2 = keyboard.nextdouble(); "Enter the length of side 3 " + "of the triangle: "); side3 = keyboard.nextdouble(); //call the perimeter method and store the result //in the value "The perimeter of the " + "triangle is " + value); default: Code Listing 5.1 continued on next page.

Gaddis_516907_Java 4/10/07 2:10 PM Page 49 Chapter 5 Lab Methods 49 } "You did not enter a valid choice."); //consumes the new line character after the number keyboard.nextline(); } } "Do you want to exit the program " + "(Y/N)?: "); String answer = keyboard.nextline(); letter = answer.charat(0); }while (letter!= Y && letter!= y );