CMPS 260 programming projects 5 and 6(1 & 2)
toThePowerOf is a method that accepts two int arguments and returns the value of the first parameter raised to the power of the second. An int variable cubeSide has already been declared and initialized. Another int variable, cubeVolume, has already been declared. Write a statement that calls toThePowerOf to compute the value of cubeSide raised to the power of 3 and that stores this value in cubeVolume. Assume that toThePowerOf is defined in the same class that calls it.
cubeVolume = toThePowerOf(cubeSide, 3);
add is a method that accepts two int arguments and returns their sum. Two int variables, euroSales and asiaSales, have already been declared and initialized. Another int variable, eurasiaSales, has already been declared. Write a statement that calls add to compute the sum of euroSales and asiaSales and that stores this value in eurasiaSales. Assume that add is defined in the same class that calls it.
eurasiaSales = add(euroSales, asiaSales);
Write a method that computes future investment value at a given interest rate for a specified number of years. The future investment is determined using the formula in Programming Exercise 2.21. Use the following method header: public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years) For example, futureInvestmentValue(10000, 0.05/12, 5) returns 12833.59. Write a test program that prompts the user to enter the investment amount (e.g., 1000) and the interest rate (e.g., 9%) and prints a table that displays future value for the years from 1 to 30, as shown below: The amount invested: 1000 Annual interest rate: 9 Years Future Value 1 1093.80 2 1196.41 ... 29 13467.25 30 14730.57 Class Name: Exercise06_07
import java.text.DecimalFormat; import java.util.Scanner; public class Exercise06_07 { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); // Enter loan amount System.out.print( "Enter investment amount, for example 100: "); double investmentAmount = input.nextDouble(); // Enter yearly interest rate System.out.print( "Enter yearly interest rate, for example 5.25: "); double annualInterestRate = input.nextDouble(); System.out.printf("%-5s%20s\n", "Years", "Future Value"); for (int i = 1; i <= 30; i++) { double futureValue = futureInvestmentValue(investmentAmount, annualInterestRate / 1200, i); System.out.printf("%-5d%20.2f\n", i, futureValue); } } public static double futureInvestmentValue(double investmentAmount, double monthlyInterestRate, int years) { return investmentAmount * Math.pow(1 + monthlyInterestRate, years * 12); } }
Assume letters A, E, I, O, and U as the vowels. Write a program that prompts the user to enter a string and displays the number of vowels and consonants in the string. Sample Run: Enter a string: Programming is fun The number of vowels is 5 The number of consonants is 11 Class Name: Exercise05_49
import java.util.Scanner; public class Exercise05_49 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a string: "); String s = input.nextLine(); int countVowels = 0; int countConsonants = 0; for (int i = 0; i < s.length(); i++) { char temp = Character.toUpperCase(s.charAt(i)); if (temp == 'A' || temp == 'E' || temp == 'I' || temp == 'O' || temp == 'U') countVowels++; else if (Character.isLetter(temp)) countConsonants++; } System.out.println("The number of vowels is " + countVowels); System.out.println("The number of consonants is " + countConsonants); } }
max is a method that accepts two int arguments and returns the value of the larger one. Four int variables, population1, population2, population3 and population4 have already been declared and initialized. Write an expression (not a statement!) whose value is the largest of population1, population2, population3 and population4 by calling max. Assume that max is defined in the same class that calls it.
max(max(population1, population2), max(population3, population4))
(Sum series) Write a method to compute the following series: m(i) = 1/2 + 2/3 +......+ i/(i+1) Write a test program that displays the following table: i m(i) 1 0.5000 2 1.1667 ... 19 16.4023 20 17.3546 Class Name: Exercise06_13
public class Exercise06_13 { public static void main(String[] args) { System.out.printf("%4s%20s\n", "i", "m(i)"); for (int i = 1; i <= 20; i++) System.out.printf("%4d%20.4f\n", i, m(i)); } public static double m(int i) { double sum = 0; for (int k = 1; k <= i; k++) sum += k / (k + 1.0); return sum; } }
write the definition of a method powerTo, which receives two parameters. the first is a double and the second is an int. the method returns a double. if the second parameter is negative, the method returns zero. otherwise it returns the value of the first parameter raised to the power of the second parameter.
public static double powerTo(double x, int n) { double prod = 1.0; if (n < 0) return 0.0; while (n > 0) { prod *= x; n--; } return prod; }
write the definition of a method add, which receives two int parameters and returns their sum.
public static int add(int a, int b) { return a + b; }
write the definition of a method min that has two int parameters and returns the smaller.
public static int min(int a, int b) { return a < b ? a : b; }
write the definition of a method twice, which receives an int parameter and returns an int that is twice the value of the parameter.
public static int twice(int x) { return 2 * x; }
Write the definition of a method dashedLine, with one parameter, an int. If the parameter is negative or zero, the method does nothing. Otherwise it prints a complete line terminated by a newline to standard output consisting of dashes (hyphens) with the parameter's value determining the number of dashes. The method returns nothing.
public static void dashedLine(int n) { for (int i = 0; i < n; i++) System.out.print('-'); System.out.println(); }
Write the definition of a method printDottedLine, which has no parameters and doesn't return anything. The method prints to standard output a single line (terminated by a new line) consisting of five periods.
public static void printDottedLine() { System.out.println("....."); }
Write the definition of a method printGrade, which has a char parameter and returns nothing. The method prints on a line by itself the message string Grade: followed by the char parameter (printed as a character) to standard output. Don't forget to put a new line character at the end of your line.
public static void printGrade(char ch) { System.out.println("Grade: " + ch); }
write the code for invoking a method named sendObject. there is one argument for this method which is of type Customer. assume that there is a reference to an object of type Customer, in a variable called John_Doe. use this reference as your argument. assume that sendObject is defined in the same class that calls it.
sendObject(John_Doe);
write the code for invoking a method named sendSignal. There are no arguments for this method. assume that sendSignal is defined in the same class that calls it.
sendSignal();