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

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.
Throwing Exceptions - JAVA File Factorials contains a program that calls the factorial method of theMathUtils...
Throwing Exceptions - JAVA File Factorials contains a program that calls the factorial method of theMathUtils class to compute the factorials of integers entered by the user. Save these files to your directory and study the code in both, then compile and run Factorials to see how it works. Try several positive integers, then try a negative number. You should find that it works for small positive integers (values < 17), but that it returns a large negative value for...
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called  Dog that contains instance data that represent the dog's name and age.   define the Dog constructor to accept and initialize instance data.   create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age).   Include a toString...
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called Dog that contains instance data that represent the dog's name and age. define the Dog constructor to accept and initialize instance data. create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age). Include a...
JavaScript 1. FizzBuzz Submit js file with functioning code Write a program that uses console.log to...
JavaScript 1. FizzBuzz Submit js file with functioning code Write a program that uses console.log to print all the numbers from 1 to 120, with two exceptions. For numbers divisible by 4, print "Fizz" instead of the number, and for numbers divisible by 10 (and not 4), print "Buzz" instead. When you have that working, modify your program to print "FizzBuzz", for numbers that are divisible by both 4 and 10 (and still print "Fizz" or "Buzz" for numbers divisible...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT