These are the 3 questions and I am a beginner at Java so I'm not positive if I'm answering the questions correctly/doing what the question s asking...
Thank you in advance for any help!
(1)- Write a program that uses a method to calculate the average of three double numbers. The
method should receive three parameters and return the average of them. Call the method from your
main() method using any three numbers you wish, and print the result. There is no user input for this
problem.
CODE
// Author: Mark Einsiedel (mse12)
public class Lab4_Problem1 {
public static void main(String[] args) {
// Write a program that uses a method to calculate the average of three double numbers. The
// method should receive three parameters and return the average of them. Call the method from your
// main() method using any three numbers you wish, and print the result. There is no user input for this
// problem.
double number1 = 1.55;
double number2 = 2.55;
double number3 = 3.55;
double avg = average(number1, number2, number3);
System.out.println("The average of the 3 numbers is " + avg);
}
public static double average(double number1, double number2, double number3) {
double result;
result = (number1 + number2 + number3) / 3;
return result;
}
}
(2) - Write your own println() method that receives one double parameter and does not
return anything. Call this new method from inside your main() method to print out a real-valued
number (such as 3.14). There is no user input for this problem. Note that you will need to use
System.out.println() inside the method. However you can now print anything from the
main() method with a little less typing.
CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication50;
/**
*
* @author Mark Einsiedel (mse12)
*/ //Lab 4 Problem 2
public class Lab4_Problem2 {
/**
* @param args the command line arguments
*/
public static void main (String [] args) {
}
public static double nPrintln(String message, double x) {
// Write your own println() method that receives one double parameter and does not
// return anything. Call this new method from inside your main() method to print out a real-valued
// number (such as 3.14). There is no user input for this problem. Note that you will need to use
// System.out.println() inside the method. However you can now print anything from the
// main() method with a little less typing.
double number = 3.14;
double result = number;
System.out.println("The number is " + number);
return result;
}
}
(3) - We have been using methods from Java libraries. As two examples, consider the methods
System.out.println()and Math.random(). The name of the library is always required,
followed by the dot separator “.”, followed by the actual method name. For this problem, you will use
the RandomCharacter library created by the author of your textbook and shown in Listing 5.8 (p 158) of
your textbook. Perform the following steps:
a. To use a custom library like this, you must compile it. To save time, the .java file for this library
is posted on our course website. Download the file and compile it.
b. In a separate .java file, create a program that uses the RandomCharacter library to print the
following:
i. One random lowercase letter.
ii. One random uppercase letter.
iii. One random numerical digit.
c. Use the DrJava debugger to step through your program and record each line number that the
debugger stops at, particularly when it jumps to the RandomCharacter.java file. Include the line
numbers in a comment somewhere in your program for your TA to grade this part. Remember
that you can display line numbers in DrJava by going to Edit/Preferences/DisplayOptions and
checking “Show All Line Numbers”. Below is an example (but it is incorrect!):
// Lab4.java: 3, 4, 5
// RandomCharacter.java: 4, 5, 6, 7, 8
// Lab4.java: 6, 7
// RandomCharacter.java: 4, 5, 6, 7, 8
Hints: Look at the methods in the code to see what parameters, if any, are needed when calling each
method. Look at the highlighted code on line 9 of the example TestRandomCharacter.java (Listing 5.9,
p 158).
CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication51;
/**
*
* @author Mark Einsiedel (mse12)
*/
public class Lab4_Problem3 {
/**
* @param args the command line arguments
*/
// Create a program that uses the RandomCharacter library to print the
// following:
// i. One random lowercase letter.
// ii. One random uppercase letter.
// iii. One random numerical digit.
public static char getRandomCharacter(char ch1, char ch2) {
return (char)(ch1 + Math.random() * (ch2 - ch1 +1));
}
/** Generate one random lowercase letter (part i) */
public static char getRandomLowerCaseLetter() {
return getRandomCharacter('a', 'z');
}
/** Generate one random upercase letter (part ii) */
public static char getRandomUpperCaseLetter() {
return getRandomCharacter('A', 'Z');
}
/** Generate one random numerical digit (part iii) */
public static char getRandomDigitCharacter() {
return getRandomCharacter('0', '9');
}
}