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 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...
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.
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...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Java question- Write a java program to process the number.txt file. Then count the numbers and...
Java question- Write a java program to process the number.txt file. Then count the numbers and calculate the total average, even numbers average, odd number average, then print the corresponding information. The result should be displayed in the following format there are XX numebers in the file there are xx even numbers there are xx odd numbers the total number average is xx the odd number average is xx the even number average is xx I am having trouble using...
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...
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
One file java program that will simulate a game of Rock, Paper, Scissors. One of the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report...
Write a java program calls the following methods: a. printStars(): Takes an int (n) as parameter...
Write a java program calls the following methods: a. printStars(): Takes an int (n) as parameter and prints n stars (*) using for loop. ex. 6 ******
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT