I need java code for the following problem.
Please find the answer below, I have mentioned all the details in the comments:
Main.java public class Main { public static void main(String[] args) { //test for all the methods System.out.println("Test for cubeIt"); System.out.println("return value of cubeIt(2): " + Numbers.cubeIt(2)); System.out.println("return value of cubeIt(3): " + Numbers.cubeIt(3)); System.out.println(); System.out.println("Test for randomInRange"); System.out.println("return value of randomInRange(2): " + Numbers.randomInRange(10,20)); System.out.println("return value of randomInRange(2): " + Numbers.randomInRange(1,5)); System.out.println(); System.out.println("Test for countA"); System.out.println("return value of countA(\"RANDOM\"): " + Characters.countA("RANDOM")); System.out.println("return value of countA(\"ABCABCAA\"): " + Characters.countA("ABCABCAA")); System.out.println(); System.out.println("Test for countA"); System.out.println("return value of multiConcat(\"hi\",3): " + Characters.multiConcat("hi",3)); } } class Numbers{ public static int cubeIt(int x){ //returnt the x^3 using pow method return (int)Math.pow(x,3); } public static int randomInRange(int min,int max){ int val; //get the value using Math.Random() val = min + (int)(Math.random() * ((max - min) + 1)); return val; } } class Characters{ public static int countA(String val){ int count = 0; //count each character by comparing it with the 'A' for (int i=0; i<val.length(); i++) { // checking character in string if (val.charAt(i) == 'A') count++; } return count; } public static String multiConcat(String s, int count){ //get the string builder object StringBuilder sb = new StringBuilder(); while(count-- > 0){ //conacat for count times sb.append(s); } //return string value return sb.toString(); } }
Output:
I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...
Lab 7: Methods 1. Write a Java program 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...
By using Java public static methods solve following problem Write a method called vowelCount that accepts a String as its only parameter, and returns an array int[5] containing the count of vowels a,e,i,o,u in that String, using ignoreCase. For example, vowelCount("") returns {0,0,0,0,0}, and for the callvowelCount("Bill Iverson") your method returns {0,1,2,1,0} because there is one 'e' and two 'i' vowels (ignore case) and one 'o' with zero counts for 'a' and 'u' vowels.
Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a method that computes and returns the area of a square using the following header: public static double area ( double side) Write a main method that prompts the user to enter the side of a square, calls the area method then displays the area. Question 1a: Write a method that converts miles to kilometers using the following header: public static double convertToKilometers (double miles)...
Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon that accepts an array of integers as its only parameter, and returns the int that occurs most frequently. Break ties by returning the lower value For example, {1,2,2,3,4,4} would return 2 as the most common int. B. Write mostCommon (same as above) that accepts an array of doubles, and returns the double that occurs most frequently. Consider any double values that are within 0.1%...
java 1. Write a method header on line three with the following specs: Returns: a boolean Name: beTrue Parameters: none public class Main { { return true; } } 2. Write a method header on line two with the following specs: Returns: a String Name: makeCapital Parameters: a String named "name" You should not be writing code on any line other than #2 public class Main { { String ans = name.toUpperCase();...
Write a complete Java program called MethodTest according to the following guidelines. The main method hard-codes three integer values into the first three positions of an array of integers calls a method you write called doubleEachValue that takes an array of integers (the one whose values you hard-coded in main) as its only argument and returns an ArrayList of integers, with each value in returned ArrayList equal to double the correspondingly indexed value in the array that is passed in...
1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...
In Java write the following array- processing methods into the same application, Lab13.java. Use the main method in this application to test your methods. 1) Write the void method, shiftLeft, which accepts an array of int and changes the elements of this array so that the index of each element is now less by one and the last element of the array is now zero. For example, if the parameter array is {7, 3, 2, 9, 5}, then when this...
Write a java Program Initialize Array. Write a Java method initArray() with the following header to initialize a one-dimensional array a such that the element values (integers) are twice the index value. For example, if i = 23, then a23 = 46. The function is void with one parameter -- the integer array of a[]. Then write a main() with an int[] array2i size 100 to call
Use Java to answer the question (Occurrences of a specified character) Write a method that finds the number of occurrences of a special character in a string using the following header: public static int count (String str, char a) For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a string followed by a character then displays the number of occurrences of the character in the string. In addition to the requirements described in...