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...
java Goal: This lab will give you practice writing code that uses inheritance and Java interfaces....
java Goal: This lab will give you practice writing code that uses inheritance and Java interfaces. Please make sure you have a partner for this lab. No code is provided with this lab. Write code to experimentally resolve the following questions about Java. You will need to show your code to the TA or lab assistant. Part I: Assignments and Casting (1 point) ------------------------------------------ Let Y be a subclass of X, and let y and x be variables of classes...
Suppose you are writing a program to compute the semester GPA for a person based on...
Suppose you are writing a program to compute the semester GPA for a person based on knowing the grades earned and the number of credit hours for each course taken. What type of tests would you need to create to check that your program was working correctly? Be specific, and remember to keep in mind tests that might contain unexpected input. How would your program react to those? After you have made your post, you need to provide thoughtful comments...
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...
JAVA CODE Write two definitions of a boolean method called equals(). The method compares the instance...
JAVA CODE Write two definitions of a boolean method called equals(). The method compares the instance variables of the class for equality. One is in the Purchase class and the other is a static method of the main. Give sample calls for each. public class PurchaseDemo { public static void main(String[] args) { Purchase oneSale = new Purchase(); oneSale.readInput(); oneSale.writeOutput(); System.out.println("Cost each $" + oneSale.getUnitCost()); System.out.println("Total cost $" + oneSale.getTotalCost()); } } import java.util.Scanner; /**    Class for the purchase...
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...
. In this lab you will be making a text-based calendar program that looks like: JANUARY...
. In this lab you will be making a text-based calendar program that looks like: JANUARY 2018 S M Tu W Th F S 1 2 3 4 5 6    7 8 9 10 11 12 13 14 15 16 17 18 19 20    21 22 23 24 25 26 27 28 29 30 31 Your calendar should be able to print months in the future and in the past. Activity #1 In C++ create a calendar program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT