Colorado State University Logo | CS 150: Culture and Coding (AUCC 3B/GT-AH3) Colorado State University Logo | CS 150: Culture and Coding (AUCC 3B/GT-AH3)
CS 150: Culture and Coding (AUCC 3B/GT-AH3)
Computer Science

Lab 06 - Even More Fun With Methods Lab

For this lab you are going to practice writing method signatures, implementing if statements, and improve your skills with for loops. The methods you will be writing have nothing to do with each other but allow you to use the skills you have gained so far to solve a series of small problems.

Power

For this method you will practice using methods from the Math class.

The first step you must take is to write the method signature. It is a public static method that returns a double. It is called power and takes in two parameters of type double.

The method signature could look like this for example:

public static double power(double num1, double num2)

In this method you want to take the two doubles in the parameters and floor them using Math.floor(). When you use Math.floor() it takes a decimal number and rounds it down. So 3.7 would become 3.0.

After you floor the two numbers you want to return num1 to the power of num2 using Math.pow().

If you want an example of how methods in the Math class are used click here

Testing power

Once you have finished writing the method you should uncomment the lines of code provided for you in main that have power in them. Remember it is important to incrementally test your program as you write it because then you will have less errors to sort out when you get to the end. Feel free to add your own tests to main.

Summing Numbers

For this the method you must add up all the numbers from 0-9 using a for loop.

The first thing you must do is write the method signature. It is a public static method called sum that returns an int. It does not take any parameters.

The for loop should start at the first number so 0 and should increment by one until it reaches 9. Inside the loop you will want to add the numbers to a variable of type int that you will return at the end of the method.

Testing sum

Once you have finished writing the method you should uncomment the code provided for you in the main method that has sum in it to test you the method you just wrote. There is only one comment because sum only performs one action and does not take any parameters.

Comparing String Length

For this method you will take two Strings and compare their lengths using .length() and return whichever String is longer.

The method is a public static method called compareLength that returns a String. It takes in two parameters that are both Strings.

You must use if statements to compare the lengths of the two Strings from the parameter and return whichever one is longer. If the two Strings are of equal length return “They are equal”.

Testing compareLength

Again once you have finished writing the method you should uncomment the code provided for you in the main method to test the method you have just written. Make sure to uncomment the three Strings along with the provided print statements. Feel free to add your own tests or change the Strings in the main method.

Determining Leap Years

For this method you will write code to determine whether or not a given year is a leap year.

The method is a public static method called isLeapYear that returns a boolean. It takes in a parameter of type int. If the year is a leap year return true otherwise return false.

A year is a leap year if it is:

  • divisible by 4 and not divisible by 100
  • divisible by both 100 and by 400

Hint: in order to check if a number is divisible you use the modulo function (%). If you want to learn more about the modulo function click here

Testing isLeapYear

Uncomment the code provided for you in the main method with isLeapYear in them. Feel free to add your own tests or change the provided tests to test with new numbers.

Reversing a String

For this method you will return the reverse of a String.

The method is a public static method called reverse that returns a String. It takes a parameter of type String. You will want to create a String variable in the method to hold the reversed String.

When reversing a String in a for loop you want your loop variable to start at one less then the length of the String and end at the beginning of the String. The reason you start at one less then the length of is because String index’s are zero based. You will then want to decrement (using –) your loop variable.

Once you have set up your for loop concatenate each character onto your String that contains the reversed String and return that String at the end of the method.

Testing reverse

Uncomment the code provided for you in the main method with reverse in it to test the method you just wrote. Feel free to add your own test cases to test the method.

Turning in

Once you have run the program and are satisfied it is working, you may submit for grading. Note that you only get five submission attempts, so make sure it is mostly working before you submit. We will bypass the main method in our tests, running our own tests on each method you wrote.

Computer Science Department

279 Computer Science Building
1100 Centre Avenue
Fort Collins, CO 80523
Phone: (970) 491-5792
Fax: (970) 491-2466

CS 150: Culture and Coding (AUCC 3B/GT-AH3)

Survey of computer science, formal logic, and computational thinking. Explores the historical, gender, and cultural perspectives on the role of technology in society. Includes learning a basic programming language. Students will be expected to write small programs, and construct written arguments on ways in which technology influences our modern culture. Previous computer science experience not necessary.