Question

LAB PROMPT: Lab 06 For this lab you are going to practice writing method signatures, implementing...

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

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

}
  
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

import java.lang.*;
import java.util.*;
public class MethodFun {
// power method
public static double power(double num1,double num2)
{
    double a,b,c;
    a=Math.floor(num1);
    b=Math.floor(num2);
    c=Math.pow(a,b);
    return c;
}

//sum method
public static int sum()
{
   int s=0;
   for(int i=0;i<=9;i++)
   {
       s+=i;
   }
   return s;
}
//compareLength method
public static String compareLength(String a,String b)
{
   if(a.length()==b.length())
   {
       return "Both are equal";
   }
   else if(a.length()>b.length())
   {
       return a;
   }
   else
   {
       return b;
   }
}
//isLeap year method
public static Boolean isLeapYear(int a)
{
   if(a%4==0)
   {
       if(a%100==0)
       {
           if(a%400==0)
           {
               return true;
           }
           else
           {
               return false;
           }
       }
       else
       {
           return false;
       }
   }
   else
   {
       return false;
   }
}
//revers method
public static String reverse(String s1)
{
   String res="";
   for(int i=s1.length()-1;i>=0;i--)
   {
       res+=s1.charAt(i);
   }
   return res;
}
public static void main(String[] args) {
//calling power method
System.out.println(power(3.4, 2) );
System.out.println(power(6.9, 4.1));
System.out.println(power(2.8, 5.3) );
System.out.println();

//calling sum method
System.out.println(sum());
System.out.println();

//calling compareLength method
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();

//calling isLeapYear method
System.out.println(isLeapYear(1800));
System.out.println(isLeapYear(1600));
System.out.println(isLeapYear(1966));
System.out.println();

//calling reverse method
System.out.println(reverse("Always"));
System.out.println(reverse("The quick brown fox"));
System.out.println(reverse("This is a String"));
System.out.println();

}


}


CODE SCREENSHOT:

1 2 3 import java.lang. *; import java.util.*; public class MethodFun { // power method public static double power (double nu

41 public static Boolean isLeapYear(int a) 42 { if(a%4==0) if(a%100==0) if(a%400==0) return true; else return false; } else r

System.out.println(); //calling sum method System.out.println(sum()); System.out.println(); 85 87 88 89 90 91 //calling compa
OUTPUT:

9.0 1296.0 32.0 That is the question Both are equal That is the question false true false syawlA xof nworb kciuq eht gnirts a

Add a comment
Know the answer?
Add Answer to:
LAB PROMPT: Lab 06 For this lab you are going to practice writing method signatures, implementing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • LAB PROMPT: Lab 07 Hello future bakers and artisans of the fine gourmet. You love to...

    LAB PROMPT: Lab 07 Hello future bakers and artisans of the fine gourmet. You love to throw parties, and what goes better at a party than cake! The problem is that you only have enough flour to bake a limited number of cakes. In this lab, you will write a small program to figure out if you have enough flour to bake the number of cakes needed for the people attending. Step 1 - using final At the top of...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • IT Java code In Lab 8, we are going to re-write Lab 3 and add code...

    IT Java code In Lab 8, we are going to re-write Lab 3 and add code to validate user input. The Body Mass Index (BMI) is a calculation used to categorize whether a person’s weight is at a healthy level for a given height. The formula is as follows:                 bmi = kilograms / (meters2)                 where kilograms = person’s weight in kilograms, meters = person’s height in meters BMI is then categorized as follows: Classification BMI Range Underweight Less...

  • You will be writing some methods that will give you some practice working with Lists. Create...

    You will be writing some methods that will give you some practice working with Lists. Create a new project and create a class named List Practice in the project. Then paste in the following code: A program that prompts the user for the file names of two different lists, reads Strings from two files into Lists, and prints the contents of those lists to the console. * @author YOUR NAME HERE • @version DATE HERE import java.util.ArrayList; import java.util.List; import...

  • JAVA Programming Produce a method that reads in a set of values (double) from the user...

    JAVA Programming Produce a method that reads in a set of values (double) from the user and returns them. Use this header: public static double[] getNumsFromUser(String msg1, String msg2) The implementation of the method should start with a message to input the total number of array elements, followed by a message to enter all the array elements. The method returns the array of elements. The two strings msg1 and msg2 that are passed to the method as parameters will be...

  • Hi I really need help with the methods for this lab for a computer science class....

    Hi I really need help with the methods for this lab for a computer science class. Thanks Main (WordTester - the application) is complete and only requires uncommenting to test the Word and Words classes as they are completed. The needed imports, class headings, method headings, and block comments are provided for the remaining classes. Javadocs are also provided for the Word and Words classes. Word The Word class represents a single word. It is immutable. You have been provided...

  • Please answer using java. For this lab you will practice accessing variables in an array and...

    Please answer using java. For this lab you will practice accessing variables in an array and determining if the values are odd. You will finish the method makeOddArray. You will store only the odd values in another array and return that array. Hint: you will need to keep track of how many items you have currently put in the new array and increment that value as you add. This will keep track of your index for the new array. Below...

  • I need to create a code for this prompt: In this project we will build a...

    I need to create a code for this prompt: In this project we will build a generic UserInput class for getting keyboard input from the user. Implementation: The class UserInput is a 'Methods only' class, and all the methods should be declared static. Look at the TestScanner.java program at the bottom of this page that inputs a string, int and double. It shows you how to use Scanner class to get input from the keyboard. Write FOUR simple methods, one...

  • Part 1- Method practice Create a main method. In there, make an array of 100 random...

    Part 1- Method practice Create a main method. In there, make an array of 100 random integers(numbers between 0 and 1000). Create a method to find average of the array. Pass the array and return the number(double) that is the average In the main method, call the method created in Step 2. Print out the average Create a method to find the min value of the array. Pass the array and return the int that is the smallest.(pass the value...

  • Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the...

    Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the following data entry error conditions: A number less than 1 or greater than 12 has been entered for the month A negative integer has been entered for the year utilizes a try and catch clause to display an appropriate message when either of these data entry errors exceptions occur. Thank you let me know if you need more info import java.util.*; //Class definition public...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT