Question

Please write a code in Java where it says your // your code here to run...

Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed.

_ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please write the code according to functions assigned for the assignment.  

You can use these functions in the assignment.

Modifier and Type Method
static int bestGuess​(int firstGuess, int secondGuess, int answer)
static double bigProduct​(double firstNumber, double secondNumber)
static int bigSum​(int firstNumber, int secondNumber)
static int countWord​(java.lang.String word, java.lang.String text)
static boolean hasBlank​(java.lang.String original)
static boolean hasWord​(java.lang.String word, java.lang.String text)
static boolean isCapital​(java.lang.String original)
static boolean isFloat​(java.lang.String firstString)
static boolean isInt​(java.lang.String firstString)
static int topSum​(int firstNumber, int secondNumber, int thirdNumber, int fourthNumber)


/**
   This exercise involves implementing several methods. Stubs for each method
   with documentation are given here. It is your task to fill out the code in
   each method body so that it runs correctly according to the documentation.

   You can run this file by compiling TestMethods1.java
   It will call this program and run it, validating the test you choose.

   Example inputs with output are provided in the comments before each method.
   At a minimum, your solutions must pass these tests. My version of TestMethods1
   contains more than the examples given or those cases in your version of
   TestMethods1. Therefore, hardcoding the answers will not pass.
**/

//imports are not allowed in this exercise.

public class ExerciseMethods1{

   /**
       Boolean function returns true if the given string begins with a capital letter.

       isCapital("Robert") returns true
       isCapital("CSCE111") returns true
       isCapital("bicycle") returns false
       isCapital("42") returns false
       isCapital("") returns false
       isCapital(" home") returns false
   isCapital("TAMU")) returns true
       @param original String,
       @return true if the first letter is a capital letter. False otherwise.
   */
   public static boolean isCapital(String original)
   {
       //your code here
return false;
   }//end isCapital

   /**
       Int function that returns true if the string has a blank.

       hasBlank("Robert Lightfoot") returns true
       hasBlank("Today is the best ever.") returns true
       hasBlank("abcdefghijklmnopqrstuvwxyz") returns false
       hasBlank("2590") returns false
       hasBlank("") returns false
       hasBlank(" home") returns true
       hasBlank("TAMU")) returns false
       @param original String,
       @return true if a blank is contained. False otherwise.
   */
public static boolean hasBlank(String original)
{
       //your code here
       return false;
}// end hasBlank

   /**
       isInt function returns true if the string is an integer. false otherwise

       isInt("12564") returns true
       isInt("-567") returns true
       isInt("23.9") returns false
       isInt("twenty two") returns false
       @param firstString String,
       @return true only if the input string only contains digits 0-9 and/or a leading "-".
   */
public static boolean isInt(String firstString)
{
       //your code here
return false;
}// end isInt

   /**
       isFloat function returns true if the string is a floating point number. false otherwise
       Note, like doubles, integers are acceptable.

       isFloat("12564") returns true
       isFloat("-567") returns true
       isFloat("23.9") returns true
       isFloat("-56.7") returns true
       isFloat("-567h") returns false
       isFloat("twenty two") returns false
       @param firstString String,
       @return true only if the input string only contains an optional leading "-"digits 0-9
          and/or a "." followed by optional trailing digits.
   */
       public static boolean isFloat(String firstString)
       {
           //your code here
               return false;
       }// end isFloat

       /**
          If the word is contained in the text, return true. Otherwise return false.
          Capitalization is not cosidered. Consider the whole word,
          "The" is not contained in "There are no more bananas."

          hasWord("Nice", "Today is a nice day.") returns true
          hasWord("The", "There are no more bananas!") returns false
          // hasWord("bananas", "There are no more bananas?") returns true
          hasWord("any-string", "I can find any-string any-where.") returns true
           hasWord("I", "I can find any-string any-where.") returns true
          hasWord("Army", "I have decided to join the Navy.") returns false
           myFunctions.hasWord(" ", "") returns false
          @param word String,
          @param text String,
          @return true if the word in contained in the text.
      */
public static boolean hasWord(String word, String text)
{
//your code here
return false;
}//end hasWord

   /**
       If the word is contained in the text, return the number of times it is contained.
       Otherwise return 0. If there is no word, or no text, return -1.
       Look at whole words, "the" is not contained in "these".
       Capitalization is not cosidered.

       hasWord("the", "The weather outside is frightful.") returns 1
       hasWord("any-where", "I can find any-string any-where.") returns 1
       hasWord("farm","Old McDonald had a farm. EI EI O. And on that farm he had a cow.") returns 2
       hasWord("do","I love dogs, do you?") returns 1
       hasWord("Army", "I have decided to join the Navy.") returns 0
       hasWord("","This is the fist line of over 1000 lines.") returns -1
       @param word String,
       @param text String,
       @return returns the count of word in text, or -1 if either is null.
       */
       public static int countWord(String word, String text)
       {
               //your code here
               return -1;
       }//end countWord

   /**
       Given three integers, return whichever is closer to the answer.
       In the event of a tie return 0.
       Note that Math.abs(n) returns the absolute value of a number.

       bestGuess(8, 13, 10) returns 8
       bestGuess(13, 8, 10) returns 8
       bestGuess(13, 7, 10) returns 0
       @param firstGuess int number,
       @param secondGuess int number,
       @param answer int number,
       @return of two int guess values, whichever is closer to the answer.
   */
public static int bestGuess(int firstGuess, int secondGuess, int answer)
{
       //your code here
return 0;
}//end bestGuess

   /**
       Method topSum returns the sum of the largest two numbers sent.

       topSum(3, 3, 3, 3) returns 6
       topSum(1, 10, 45, 2) returns 55
       topSum(-6, 2, -45, -8) returns -4
       topSum(3, 113, 2, 200) returns 313
       @param firstNumber int number,
       @param secondNumber int number,
       @param thirdNumber int number,
       @param fourthNumber int number,
       @return the sum of the largest two numbers sent.
   */
public static int topSum(int firstNumber, int secondNumber, int thirdNumber, int fourthNumber )
{
//your code here
return 0;
}// end topSum

   /**
       Method bigSum returns the sum of all numbers between the first number and the second
       number inclusive. Works in either direction.

       bigSum(3, 3) returns 6
       bigSum(1, 10) returns 55
       bigSum(-6, 1) returns -20
       bigSum(5, 3) returns 12
       @param firstNumber int number,
       @param secondNumber int number,
       @return the sum of the numbers from the first to the second.
   */
   public static int bigSum(int firstNumber, int secondNumber)
   {
   //Your code here
   return 0;
   }//end bigSum

   /**
       Method bigProduct returns the product of all numbers between the first number and the second
       number inclusive. Works in either direction.

       bigProduct(3, 3) returns 9
       bigProduct(1, 10) returns 362880
       bigProduct(-6, 1) returns 0
       bigProduct(5, 3) returns 60
       @param firstNumber double number,
       @param secondNumber double number,
       @return the product of the numbers from the first to the second.
   */
   public static double bigProduct(double firstNumber, double secondNumber)
   {
   //Your code here
   return 0.0;
   }//end bigProduct

}//end ExerciseMethods1

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

I have directly implemented the functions, you can use these as you want and call them in main by passing appropriate paramenters.

public class JavaMeth

{

static int bestGuess(int a,int b,int ans)
   {
       int gs1 = Math.abs(a-ans);
       int gs2 = Math.abs(b-ans);
       if(gs1<gs2)
           return a;
       else if(gs2<gs1)
       {
           return b;
       }
       else
           return 0;
   }

  static double bigProduct(double firstNumber, double secondNumber)
   {
       double total=1;
       if(firstNumber==secondNumber)
       {
           return firstNumber*secondNumber;
       }
       else if(firstNumber<secondNumber)
       {
           for(double i=firstNumber;i<=secondNumber;i++)
           {
               total=total*i;
           }
           return total;
       }
       else
       {
           for(double i=secondNumber;i<=firstNumber;i++)
           {
               total=total*i;
           }
           return total;
       }
      
   }

  static int bigSum(int firstNumber, int secondNumber)
   {
       int total = 0;
       if(firstNumber==secondNumber)
       {
           return firstNumber+secondNumber;
       }
       else if(firstNumber<secondNumber)
       {
           for(int i=firstNumber;i<=secondNumber;i++)
           {
               total = total+i;
           }
           return total;
       }
       else
       {
           for(int i=secondNumber;i<=firstNumber;i++)
           {
               total = total+i;
           }
           return total;
       }
   }

  static int countWord(String word,String text)
   {
       int count = 0;
       String textarr[] = text.split(" ");
       for(int i=0;i<textarr.length;i++)
       {
           if(textarr[i].equals(word))
           {
               count++;
           }
       }
       if(count==0)
       {
           return -1;
       }
       else
       {
           return count;
       }
   }

  static boolean hasBlank(String orignal)
   {
       String check[]=orignal.split(" ");
       if(check.length==1)
       {
           return true;
       }
       else
       {
           return false;
       }
   }

  static boolean hasWord(String word, String text)
   {
       word = word.toLowerCase();
       text = text.toLowerCase();
       if(text.contains(word))
       {
           return true;
       }
       else
       {
           return false;
       }
   }

  static boolean isCapital(String orignal)
   {
       char ch[]=orignal.toCharArray();
       if(Character.isUpperCase(ch[0])==true)
       {
           return true;
       }
       else
       {
           return false;
       }                  
   }

  static boolean isFloat(String firstString)
   {
       firstString = firstString.toLowerCase();
       String chfs[] = firstString.split("");
       int strcount = firstString.length(),count=0;
       String num[] = {"1","2","3","4","5","6","7","8","9","+","-","."};
       for(int i=0;i<chfs.length;i++)
       {
           for(int j=0;j<num.length;j++)
           {
               if(chfs[i].equals(num[j]))
               {
                   count++;
               }
           }
       }
       if(count==strcount)
       {
           return true;
       }
       else
       {
           return false;
       }
   }

  static boolean isInt(String firstString)
   {
       firstString = firstString.toLowerCase();
       String chfs[] = firstString.split("");
       int strcount = firstString.length(),count=0;
       String num[] = {"1","2","3","4","5","6","7","8","9","+","-"};
       for(int i=0;i<chfs.length;i++)
       {
           for(int j=0;j<num.length;j++)
           {
               if(chfs[i].equals(num[j]))
               {
                   count++;
               }
           }
       }
       if(count==strcount)
       {
           return true;
       }
       else
       {
           return false;
       }
   }

  static int topSum​ (int firstNumber, int secondNumber, int thirdNumber, int fourthNumber)
   {
       Integer arr[] = {firstNumber,secondNumber,thirdNumber,fourthNumber};
       int n = arr.length;
       for (int j = n - 1; j >= 0; j--) {    //this reorders the elements like -45,-8,-6,2
       for (int i = 0; i < j; i++) {
       if (Integer.signum(arr[i]) > Integer.signum(arr[i+1])) {
       int temp = arr[i];
       arr[i] = arr[i+1];
       arr[i+1] = temp;   
       }
       }
       }
       return arr[n-1]+arr[n-2];  
   }

}

Add a comment
Know the answer?
Add Answer to:
Please write a code in Java where it says your // your code here to run...
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
  • Please write a code in Java where it says your // your code here to run...

    Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...

  • Hey, I was wondering if there’s another way to make these methods not reliable to imports...

    Hey, I was wondering if there’s another way to make these methods not reliable to imports such as “import java.util.Arrays” and “import java.util.Hash” I am still new in coding and would like to know if there’s another way to do it! Here is the code! (Thank you in advance and I will really appreciate it :) ) /** This exercise involves implementing several methods. Stubs for each method with documentation are given here. It is your task to fill out...

  • Please help me do the java project For this project you will be reading in a...

    Please help me do the java project For this project you will be reading in a text file and evaluating it in order to create a new file that represents the Class that will represent the properties of the text file. For example, consider the following text file: students.txt ID              Name                              Age                    IsMale           GPA 1                Tom Ryan                       22                       True              3.1 2                Jack Peterson                31                       True              2.7 3                Cindy LuWho                12                       False             3.9 When you read in the header line, you...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    Given java code is below, please use it! import java.util.Scanner; public class LA2a {       /**    * Number of digits in a valid value sequence    */    public static final int SEQ_DIGITS = 10;       /**    * Error for an invalid sequence    * (not correct number of characters    * or not made only of digits)    */    public static final String ERR_SEQ = "Invalid sequence";       /**    * Error for...

  • I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][]...

    I am unsure how to add the following methods onto this code?? please help - rowValuesIncrease(int[][] t) A method that returns true if from left to right in any row, the integers are increasing, otherwise false. - columnValuesIncrease(int[][] t) A method that returns true if from top to bottom in any column, the integers are increasing, otherwise false. - isSetOf1toN(int[][] t) A method that returns true if the set of integers used is {1, 2, . . . , n}...

  • Please write where its written write your code here!! please use java for the code! please...

    Please write where its written write your code here!! please use java for the code! please use the given code and I will rate thanks Magic index in an array a[1..n] is defined to be an index such that a[ i ] = i. Given an array of integers, write a recursive method to find the first magic index from left to right. If one exists in the given array, return the index number i, otherwise return -1. Here are...

  • Create a method based program to find if a number is prime and then print all...

    Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod {    public static void main(String[] args)    {       String input;        // To hold keyboard input       String message;      // Message...

  • Topics: About Java What is a compiler, and what does it do? Characteristics of the languageStrongly...

    Topics: About Java What is a compiler, and what does it do? Characteristics of the languageStrongly typed and statically typed Everything has a data type & data types must be declared Case sensitive Object oriented System.out.print() vs. System.out.println() How to use the Scanner class to obtain user input Data typesWhat are they? Know the basic types like: int, double, boolean, String, etc. Variables What is a variable? Declarations Initialization Assignment Constants Reserved words like: What is a reserved word? Examples:...

  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g.,...

    A palindrome is a word, phrase, or sequence that reads the same backward as forward, e.g., madam or nurses run. In this program, ask the user to input some text and print out whether or not that text is a palindrome. Create the Boolean method isPalindrome which determines if a String is a palindrome, which means it is the same forwards and backwards. It should return a boolean of whether or not it was a palindrome. Create the method reverse...

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