Question

In: Computer Science

1. Write a Java program and Submit only "one .java file" calledNumbers that calls the...

1. Write a Java program and Submit only "one .java file" called Numbers that calls the following methods and displays the returned value:

o Write a method called cubeIt that accepts one integer parameter and returns the value raised to the third power as an integer.

2. Write a method called randomInRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive.

o Write a method called larger that accepts two double parameters and returns true if the first parameter is greater and false otherwise. Use the Boolean data type.

3. Write a Java program called Characters that calls the following methods and displays the returned value: o Write a method called countA that accepts a String parameter and returns the number of times the character ‘A’ is found in the string.

o Write a method called multiConcat that takes a String and integer as parameters. Return a String that consists of the string parameter concatenated with itself count times, where the count is the integer parameter. For example, if the parameter values are “hi” and 3, the return value is “hihihi”.

• Display the returned values in main.

• There is no user input.

• Set up variables and change the values to test the methods.

• Submit only one .java file

Solutions

Expert Solution

// Numbers.java (all in one java file)

public class Numbers {

      // method to return the cube of a number

      public int cubeIt(int num) {

            return num * num * num;

      }

      // method to generate and return a random number between min and max

      public int randomInRange(int min, int max) {

            // generating a value between min and max inclusive

            int num = (int) (Math.random() * (max - min + 1)) + min;

            return num;

      }

      // returns true if n1 > n2

      public Boolean larger(double n1, double n2) {

            return n1 > n2;

      }

      public static void main(String[] args) {

            // creating an object of Numbers and testing all methods

            Numbers numbers = new Numbers();

            System.out.println("cube of 5: " + numbers.cubeIt(5));

            System.out.println("cube of 0: " + numbers.cubeIt(0));

            System.out.println("random in range 10 and 100: "

                        + numbers.randomInRange(10, 100));

            System.out.println("random in range -5 and 5: "

                        + numbers.randomInRange(-5, 5));

            System.out.println("5 larger than 6: " + numbers.larger(5, 6));

            System.out.println("15 larger than 6: " + numbers.larger(15, 6));

            // creating an object of Characters and testing all methods

            Characters characters = new Characters();

            System.out.println("\nCount of 'A' in ABRACADABRA: "

                        + characters.countA("ABRACADABRA"));

            System.out.println("multiConcat (hi, 3): "

                        + characters.multiConcat("hi", 3));

      }

}

class Characters {

      // returns the count of 'A' in str (case sensitive)

      public int countA(String str) {

            int count = 0;

            // looping and counting 'A'

            for (int i = 0; i < str.length(); i++) {

                  if (str.charAt(i) == 'A') {

                        count++;

                  }

            }

            return count;

      }

      // returns str multiplied n times

      public String multiConcat(String str, int n) {

            String result = "";

            //combining str n times

            for (int i = 0; i < n; i++) {

                  result += str;

            }

            return result;

      }

}

/*OUTPUT*/

cube of 5: 125

cube of 0: 0

random in range 10 and 100: 42

random in range -5 and 5: 5

5 larger than 6: false

15 larger than 6: true

Count of 'A' in ABRACADABRA: 5

multiConcat hi, 3: hihihi


Related Solutions

Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code. Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input...
Write program#1 upload .java file. #1 Write a java program that prompts the user for input using the Scanner class. First to enter their first name. Then prompts the user to enter their last name. Then prompts the user to enter their city. Then prompts the user to enter their state. Then prompts the user to enter their zip code.   Concatenate first name and last name into full_name. Using String Class is optional. Use the String Class to replace zip...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
Submit a java file. Write a program where you input 4 numbers and the output generates...
Submit a java file. Write a program where you input 4 numbers and the output generates the average of the 4 numbers. The output may not be negative and <= 100. If any value is negative or >100, it should be replaced by the value of 30. For all such values, you will replace them with a value of 10. The program should produce the following output: Today's average amount is: $xx.xx.
Write a program in Java, that creates a Jframe with a menu containing only file. Inside...
Write a program in Java, that creates a Jframe with a menu containing only file. Inside file there should be items: Open, Save, and Save As. Selecting open prompts the user to input a file name to a txt document containing employee information and displays a Jtable with the information, which can be edited. With column headers {"First Name" , "Last Name" , "Occupation" , "Office #"} Example: Gary Osbourn Teacher 113 Michelle Ramirez Teacher 101 Ava Gomez Principal 120...
Java Write a program that will only accept input from a file provided as the first...
Java Write a program that will only accept input from a file provided as the first command line argument. If no file is given or the file cannot be opened, simply print “Error opening file!” and stop executing. A valid input file should contain two lines. The first line is any valid string, and the second line should contain a single integer. The program should then print the single character from the string provided as the first line of input...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Each student needs to submit a pdf file in ONLY ONE of the following topics: 1....
Each student needs to submit a pdf file in ONLY ONE of the following topics: 1. Modern mixing devices in the preparation of semisolids. 2. Segregation in solid dosage form manufacture. 3. Different models of rotor stator turbines. 4. Agitation patterns in solid feeders.
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to...
Write program#2 upload .java file. 2A) Write a java program that uses the Random class to generate a number in the range 21 to 64.  Print the generated number. 2B) Using the Random class and nextInt(6), rolls two die generating two random numbers each in the range 1 through 6. Total by adding the two values together. Print the value of each die, and the total value. 2C) Using the Math class and sqrt(num), calculate the square root of integer twenty-two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT