Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

LAB PROMPT: Lab 06 For this lab you are going to practice writing method signatures, implementing if statements, and improve your skills with for loops.

LAB PROMPT:

Lab 06

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().

Once you have finished writing the method you should uncomment the lines of code provided for you in main that have power in them.

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.

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.

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.

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.

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

Uncomment the code provided for you in the main method with isLeapYear in them.

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 indexs 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.

Uncomment the code provided for you in the main method with reverse in it to test the method you just wrote.

PROVIDED CODE

public class MethodFun { //TODO: power() //TODO: sum() //TODO: compareLength() //TODO: isLeapYear() //TODO: reverse() public static void main(String[] args) { // System.out.println(power(3.4, 2) + " answer should be 9.0"); // System.out.println(power(6.9, 4.1) + " answer should be 1296.0"); // System.out.println(power(2.8, 5.3) + " answer should be 32.0"); // System.out.println(); // System.out.println(sum()); // System.out.println(); // String s1 = "To be or not to be"; // String s2 = "That is the question"; // String s3 = "Whether tis nobler"; // System.out.println(compareLength(s1, s2)); // System.out.println(compareLength(s1, s3)); // System.out.println(compareLength(s2, s3)); // System.out.println(); // System.out.println(isLeapYear(1800)); // System.out.println(isLeapYear(1600)); // System.out.println(isLeapYear(1966)); // System.out.println(); // System.out.println(reverse("Always")); // System.out.println(reverse("The quick brown fox")); // System.out.println(reverse("This is a String")); // System.out.println(); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

New Trends In Databases And Information Systems Adbis 2019 Short Papers Workshops Bbigap Qauca Sembdm Simpda M2p Madeisd And Doctoral Consortium Bled Slovenia September 8 11 2019 Proceedings

Authors: Tatjana Welzer ,Johann Eder ,Vili Podgorelec ,Robert Wrembel ,Mirjana Ivanovic ,Johann Gamper ,Mikolaj Morzy ,Theodoros Tzouramanis ,Jerome Darmont

1st Edition

3030302776, 978-3030302771

More Books

Students also viewed these Databases questions