Programming Fundamentals in Java

Module [301] - Java Methods

This module will explore a new concept, methods, and its implementation in the Java programming language.

  • Textbook Reading Assignments
  • The following sections of the textbook are assigned for this module.
    • Chapter #6 – Methods

Module Learning Outcomes

In this module, you will develop further your programming skills as follows;

Tracing
Ability to trace the call to Java methods, including how the parameters are passed from the calling method to the called method and how values are returned.
Implementing
Ability to write methods with a specified name, return type, and parameters.
Testing
Ability to develop tests that are going to allow us to ensure a method defines as expected by requirements.
Debugging
Ability to troubleshoot method definitions and method calls.
Designing
Ability to identify and encapsulate into a method small fragments of code that may be reused in our programs.

  • A01 - Display matrix of 0s & 1s
  • Video Link: YouTube
  • This exercise is #6.17 in the 11th & 10th editions of our text; it was #5.17 in older editions. Write a Java method public static void printMatrix(int n) which, given a positive integer n, display a n by n matrix of random booleans displayed as 0 or 1. Use this method in the main method of a Java program which will prompt the user for a value to pass to the method.
  • A02 - MyTriangle
  • Video Link: YouTube
  • This exercise is #6.19 in the 11th & 10th editions of our text; it was #5.19 in older editions. Write a Java class named MyTriangle featuring the following methods;
    • public static boolean isValid(double side1, double side2, double side3)
    • public static double area(double side1, double side2, double side3)
    • public static void main(string[] args)
    These methods will respectively verify that the three parameters represent valid sides lengths for a triangle, compute & return its area. The main method will prompt the user for 3 values for the edges and using the other methods to first validate them and, if they are valid lengths, compute & display the area.
  • A03 - Mean & Standard Deviation
  • Video Link: YouTube
  • This exercise was #6.21 in the 10th editions of our text; it was #5.21 in older editions of our text. Since it is no longer available as of the 11th edition, please see description below. Write a Java program that generates 10 random integers between 0 and 1,000, computes their mean & standard deviation. Please note that you are expected to use existing methods from Java Math
  • A04 - Approximate Square Root
  • Video Link: YouTube
  • This exercise is #6.22 in the 11th & 10th editions of our text; it was #5.22 in older editions of our text. Write a Java method public static double sqrt(long n) which uses the Babylonian approach to approximate the square root of its parameter. The key is to start with a variable lastGuess initialize to a small positive value; e.g. 1. The nextGuess is computed as (lastGuess + n / lastGuess) / 2. If the difference between nextGuess and lastGuess is less than 0.0001, then we display the nextGuess as the approximation of the square root of n. If it is not, then lastGuess is assigned nextGuess and we keep iterating the approximation process. Make sure to also provide a main method to demonstrate that your sqrt method works.
  • A05 - Improving Current Date & Time
  • Video Link: YouTube
  • This exercise is #6.24 in the 11th & 10th editions of our text; it was #5.24 in older editions of our text. Use previous exercises we solved to write a Java program able to display the current Date & Time then determine the year, month & day.
  • A06 - Improving again current date & Time
  • Video Link: YouTube
  • This exercise is #6.24 in the 11th & 10th editions of our text; it was #5.25 in older editions of our text. Modify the previous program so that the code used to determine the current hour, minutes & second is handled by a Java method public static String converMillis(long totalMilliseconds). This method will return the information previously displayed as as String. The String will be displayed by the main method.

Each of the following links will lead you to a separate tutor applet. The tutors take usually 30 minutes to work through.

  • Problet #1 - Functions - Function Behavior
  • URL Link: See Instructor Announcements
  • This Problet tutor will allow you to understand how Java methods run
  • Problet #2 - Functions - Debugging Functions
  • URL Link: See Instructor Announcements
  • This Problet tutor will allow you to practice debugging Java methods
  • B01 - Summing the Digits of an Integer
  • This exercise is #6.2 in the 11th & 10th editions of our text.
    Write a method that takes a long parameter named n and returns, as an int value the sum of its digits. See the full description in the texbook for hints on how to write this method.
    Make sure you write a main method in your class to test this new method.
  • B02 - Sorting Three Numbers without Arrays
  • This exercise is #6.5 in the 11th & 10th editions of our text.
    Write a method with the following header:
     
    	    public static void displaySortedNumbers( double n1, double n2, double n3)
    This method will display the three provided numbers but in ascending order.
    Make sure you write a main method in your class to test this new method.
  • B03 - Converting Feet to Meters
  • This exercise is #6.9 in the 11th & 10th editions of our text.
    Write a new class which will feature the two following methods;
    	    public static double feetToMeters(double feet)
    	    public static double metersToFeet(double meters)
    As you may expect, these will implement as methods the code we already wrote in previous modules to convert values.
    Make sure you write a main method in your class to test this new method.
  • B04 - Establishing if an Integer is Prime
  • This exercise is #6.10 in the 11th & 10th editions of our text.
    Use the method isPrime from the class PrimeNumberMethod, which is provided in Listing 6.7 in your textbook, in order to find the number of primer numbers which are less than 10,000.
  • B05 - Summing Series
  • This exercise is #6.13 in the 11th & 10th editions of our text.
    Write a method which, given an integer value n, will compute and return the sum of the series:
    	    1/2 + 2/3 + ... + n/(n+1)

    Make sure you write a main method in your class to test this new method.