teaching machines

CS 145 Lab 4 – Writing methods

February 10, 2012 by . Filed under cs145, labs, spring 2012.

Prelab

Introduction

Last time we looked at calling methods that were available in the String, Scanner, and Random classes. This week, we write our own methods.

Reminder: Be sure to get your checkpoints from lab 2 checked off in the first 20 minutes of this lab. They should already be working and ready for approval. Please take the time to ask questions and resolve any lingering confusion you have about how code works.

Problems

Complete four of the five problems below. Credit is granted only where you following directions. So, ensure that you name your methods as prescribed, have the right parameters, and your return (rather than print) where it says return.

  1. Write a method composeUsername that takes two String arguments, one for the first name, one for the last name. It returns a username of the form “<lastname><first initial>” in all lowercase.
  2. Write a method that getTriangleArea that takes arguments for a triangle’s base and height and returns its area.
  3. Write a method moveMouse that takes two int arguments, creates a Robot object, and then moves the mouse to the coordinates specified by the arguments. (Eclipse will complain about the line where you create the Robot. Click on the error icon in the margin and add a throws clause.)
  4. Write a method generateWeather that generates a random weather report, listing the day’s high and low temperatures (ensure the low is lower than the high) and whether or not it is raining.
  5. Write a method getStatusBar that takes an integral argument that you may assume is between 0 and 100. Return a sequence comprised of this number of asterisks (*).

Checkpoint #1. Either properly indent your code as you go or hit Control-Shift-F in Eclipse to format it automatically. Show your TA or instructor your solutions.

TODO