Question

In: Computer Science

For this lab, you will be writing method definitions. These method definitions will be based on...

For this lab, you will be writing method definitions. These method definitions will be based on method calls I have already written for you. Do all of the following steps in order. DO NOT move onto the next step until the previous one is complete.

1. Download the starting file called Lab10.java.

2. Within this file, you will find a completely written main method. Please DO NOT modify this code. Do read the code and familiarize yourself with what it does. Specifically focus on the method calls.

3. Write the method stubs (just like we did in class) for all the methods. Once you complete this, the program will compile and you will be able to test each method as you write it completely. Remember, a method stub includes the method name, all input parameters and the output type. If the method returns a value, you should write a return statement that just returns a default value. Without this, it will not compile. Therefore, the method body should only be one line of code. In doing this you must ask yourself the following questions for each:

a. What information does the method need?

b. What information should the method return?

c. What is the method name?

4. Write a JavaDoc method header for each method. This will include a method description and any necessary parameter and return value descriptions. Make sure you view the Java documentation to see if your JavaDoc code works.

5. Write the method definitions one by one for all methods, testing after each one. Think about how the method will accomplish its task.

6. In the main method, I have included lots of tests for each method. Make sure to test each method as you write it. The main method should run completely once you have all of the method definitions correctly written.

NOTE: We will cover method overloading next class. The third method called luckySum requires this concept.

This is a java project. Since I couldn't attach the file I copy and pasted the file below

import java.util.Scanner;

/**
* This is the starting file for Lab 10.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lab10
{
/**
* The main method tests all four method definitions.
*
* @param args A string array of input arguments
*/
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
  
/**
* Method one checks to see if a number is even or odd.
*/
System.out.println("Testing method one");
  
// Test with real value
String ans1 = evenOrOdd(7); // odd
System.out.println("7 is an " + ans1 + " number.");
  
// Test with user input
System.out.println("Please enter an integer: ");
int num = input.nextInt();
String ans2 = evenOrOdd(num);
System.out.println("The integer is an " + ans2 + " number.");

/**
* Method two checks to see if either of the strings appears
* at the very end of the other string, ignoring upper/lower
* case differences (in other words, the computation should
* not be "case sensitive").
*/
System.out.println("\nTesting method two");
  
// Test with real values
boolean end1 = endOther("Hiabc", "abc"); // true
boolean end2 = endOther("AbC", "HiaBc"); // true
boolean end3 = endOther("abcXYZ", "abcDEF"); // false
System.out.println("The booleans are: " + end1 + ", " + end2 +
", " + end3);
  
// Test with user input
System.out.println();
System.out.println("Please enter the first string: ");
String first = input.next();
System.out.println("Please enter the second string: ");
String second = input.next();
  
boolean end4 = endOther(first, second);
if (end4) {
System.out.println("\nOne of the strings appears at the end of the other string.");
} else {
System.out.println("\nNeither of the strings appears at the end of the other string.");
}

/**
* This method should return the sum of three numbers.
* However, if one of the numbers is 13 then it does not
* count toward the sum and the parameters to its right
* do not count either. So, if the second number is 13,
* then the second and the third number do not count toward
* the sum.
*/
System.out.println("\nTesting method three");
  
// Test with real values
int sum1 = luckySum(4, 2, 3); // 9
int sum2 = luckySum(13, 2, 9); // 0
int sum3 = luckySum(9, 4, 13); // 13
double sum4 = luckySum(7.2, 3.4, 13.0); //10.6
double sum5 = luckySum(6.57, 13.0, 10.1); // 6.57
  
System.out.println("The lucky sums are: " + sum1 + ", " + sum2 +
", " + sum3 + ", " + sum4 + ", " + sum5);
// Test with user input
System.out.println("\nPlease enter the first number:");
int num1 = input.nextInt();
System.out.println("Please enter the second number:");
int num2 = input.nextInt();
System.out.println("Please enter the third number:");
int num3 = input.nextInt();
  
int lucky = luckySum(num1, num2, num3);
System.out.println("The lucky sum is " + lucky);
}
  
// Please write your methods here. Include JavaDoc method headers.

Solutions

Expert Solution

Explanation:I have implemented all the required methods and have commented out the luckySum() for double arguments since mentioned in the question.I have also provided the javadoc comments for each method written.I have shown the output of the program, please find the images attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.

//code starts

import java.util.Scanner;

/**
* This is the starting file for Lab 10.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lab10
{

   /**
   * The main method tests all four method definitions.
   *
   * @param args A string array of input arguments
   */
   public static void main(String[] args)
   {
       Scanner input = new Scanner(System.in);

       /**
       * Method one checks to see if a number is even or odd.
       */
       System.out.println("Testing method one");

       // Test with real value
       String ans1 = evenOrOdd(7); // odd
       System.out.println("7 is an " + ans1 + " number.");

       // Test with user input
       System.out.println("Please enter an integer: ");
       int num = input.nextInt();
       String ans2 = evenOrOdd(num);
       System.out.println("The integer is an " + ans2 + " number.");

       System.out.println("\nTesting method two");

       // Test with real values
       boolean end1 = endOther("Hiabc", "abc"); // true
       boolean end2 = endOther("AbC", "HiaBc"); // true
       boolean end3 = endOther("abcXYZ", "abcDEF"); // false
       System.out.println("The booleans are: " + end1 + ", " + end2 + ", " + end3);

       // Test with user input
       System.out.println();
       System.out.println("Please enter the first string: ");
       String first = input.next();
       System.out.println("Please enter the second string: ");
       String second = input.next();

       boolean end4 = endOther(first, second);
       if (end4)
       {
           System.out.println("\nOne of the strings appears at the end of the other string.");
       } else
       {
           System.out.println("\nNeither of the strings appears at the end of the other string.");
       }

       System.out.println("\nTesting method three");

       // Test with real values
       int sum1 = luckySum(4, 2, 3); // 9
       int sum2 = luckySum(13, 2, 9); // 0
       int sum3 = luckySum(9, 4, 13); // 13
       // double sum4 = luckySum(7.2, 3.4, 13.0); // 10.6
       // sum5 = luckySum(6.57, 13.0, 10.1); // 6.57

       System.out.println("The lucky sums are: " + sum1 + ", " + sum2 + ", " + sum3);
       // Test with user input
       System.out.println("\nPlease enter the first number:");
       int num1 = input.nextInt();
       System.out.println("Please enter the second number:");
       int num2 = input.nextInt();
       System.out.println("Please enter the third number:");
       int num3 = input.nextInt();

       int lucky = luckySum(num1, num2, num3);
       System.out.println("The lucky sum is " + lucky);
       input.close();
   }

   /**
   * This method returns a string value even or odd depending on the number
   *
   * @param i
   * @return string
   */
   public static String evenOrOdd(int i)
   {
       if (i % 2 == 0)
           return "even";
       return "odd";
   }

   /**
   * This method checks whether either of the strings occurs at the end of the
   * other string, the check performed is case insensitive.
   *
   * @param first
   * @param second
   * @return boolean
   */
   public static boolean endOther(String first, String second)
   {
       if (first.toLowerCase().endsWith(second.toLowerCase()) || second.toLowerCase().endsWith(first.toLowerCase()))
           return true;
       return false;
   }

   /**
   * This method returns the lucky sum.If one of the numbers is 13 then it does
   * not count toward the sum and the parameters to its right do not count either.
   * So, if the second number is 13, then the second and the third number do not
   * count toward the sum.
   *
   * @param d
   * @param e
   * @param f
   * @return sum
   */
   public static int luckySum(int d, int e, int f)
   {
       int sum = 0;
       if (d != 13)
       {
           sum += d;
           if (e != 13)
           {
               sum += e;
               if (f != 13)
               {
                   sum += f;
               }
           }
       }
       return sum;
   }

}

Output:


Related Solutions

For this lab you are going to practice writing method signatures, implementing if statements, manipulating Strings,...
For this lab you are going to practice writing method signatures, implementing if statements, manipulating Strings, and improve your skills with for loops. The methods you will write will be part of a short trivia game, feel free to try it out after you finish.   import java.util.Scanner; public class Trivia { //TODO: isLeapYear //TODO: isPrime //TODO: aWord //TODO: reverse public static void main(String[] args){ Scanner answers = new Scanner(System.in); int score = 0; System.out.println("What year is a Leap Year?"); //...
Writing a Modular Program in Java In this lab, you add the input and output statements...
Writing a Modular Program in Java In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared for you....
Writing a Modular Program in Python In this lab, you add the input and output statements...
Writing a Modular Program in Python In this lab, you add the input and output statements to a partially completed Python program. When completed, the user should be able to enter a year, a month, and a day. The program then determines if the date is valid. Valid years are those that are greater than 0, valid months include the values 1 through 12, and valid days include the values 1 through 31. Instructions Notice that variables have been declared...
a. How will you classify the following countries; Australia, China, US, India based on the definitions...
a. How will you classify the following countries; Australia, China, US, India based on the definitions of a small and large open economies and why?
Short Answer Writing Assignment All answers should be complete sentences. In the Week 2 Lab, you...
Short Answer Writing Assignment All answers should be complete sentences. In the Week 2 Lab, you found the mean and the standard deviation for the SLEEP variable for both males and females. Use those values for follow these directions to calculate the numbers again. (From Week 2 Lab: Calculate descriptive statistics for the variable Sleep by Gender. Sort the data by gender by clicking on Data and then Sort. Copy the Sleep of the males from the data file into...
TrackMinMax i want the generic version based on java For this lab, you will create a...
TrackMinMax i want the generic version based on java For this lab, you will create a generic version of the IntTrackMinMax class you wrote in a previous lab, called TrackMinMax. The API is: Function Signature Description constructor TrackMinMax() constructor check void check(T i) compares i to the current minimum and maximum values and updates them accordingly getMin T getMin() returns the minimum value provided to check() so far getMax T getMax() returns the maximum value provided to check() so far...
I am writing a report for a lab involving experimenting on drosophila melanogaster to study their...
I am writing a report for a lab involving experimenting on drosophila melanogaster to study their inheritance pattern. The goal of the lab was to predict the genotypes of the P cross by observing the phenotypes and their ratios in the F1 and F2 generation. Our F2 generation contained wildtype flies, white mutation, ebony mutation, and white/ebony double mutants. I am having trouble interpreting the results of the Chi square tested conducted on our flies. Our total Chi square values...
I am writing a lab report on the a nested PCR to amplify the GAPC gene...
I am writing a lab report on the a nested PCR to amplify the GAPC gene of two plants. I wrote a very small introduction on the process of nestedPCR and its specificty but i dont know what else i should incorporate. Can somone help me on how i should write my intoduction what info to use ?
This lab examines the various ways of working with arrays by writing pseudocode. Read the following...
This lab examines the various ways of working with arrays by writing pseudocode. Read the following programming problem prior to completing the lab. The American Red Cross wants you to write a program that will calculate the average pints of blood donated during a blood drive. The program should take in the number of pints donated during the drive, based on a seven-hour drive period. The average pints donated during that period should be calculated and displayed. Additionally, the highest...
Bio 101 Mitosis Lab What are the definitions of the following terms: DNA: Chromosome: Gene: Allele:...
Bio 101 Mitosis Lab What are the definitions of the following terms: DNA: Chromosome: Gene: Allele: Locus:
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT