In: Computer Science
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.
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: