import java.util.Scanner;
public class Question1
{
public static void main(String[] args)
{
Primary test = new Primary();
test.Main();
}
}
class Primary
{
double x;
double y;
double z;
double Small;
double Avg;
int dad;
final int userNumbers = 3;
Scanner in = new Scanner(System.in);
public void Main()
{
System.out.println("Please enter 3 numbers");
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
Primary test = new Primary();
test.Smallest();
test.Average();
System.out.println("The average of the numbers is:" + Avg);
System.out.println("The smallest of the numbers is:" + Small);
}
public void Smallest()
{
if(z < y && z < x)
Small = z;
if (x < z && x < y)
Small = x;
if (y < z && y < x)
Small = y;
}
public void Average()
{
Avg = x + y + z / userNumbers;
}
}
I have no clue what to do since everything ive tried either gives me an error or I'm just not doing it right. I'm not even sure I'm doing what the professor is asking me to do, and he never responds to his emails. If anyone could help me it would be greatly appreciated (And if I'm not doing what my professor is asking please let me know).
Heres his instructions + The assignment on page 248
Around Page 248. Practice Exercise E5.1.You will create one class that has three methods: main(), smallest(), and average(). main() reads the test data values and prints the results. You may want to do this in a loop. smallest() and average() do not read data values or print the results. The main() method reads the data values and prints the results.
Write the following methods and provide a program to test them.
a. double smallest(double x, double y, double z), returning the smallest of the arguments
b. double average(double x, double y, double z), returning the average of the arguments
Here try this....`
let me know how that works
import java.util.ArrayList;
import java.util.Scanner;
package javaapplication17;
public class Question1 {
public static void main(String[] args)
{
double avg, small;
Scanner in = new Scanner(System.in);
System.out.println("Please enter 3 numbers");
double x = in.nextInt();
double y = in.nextInt();
double z = in.nextInt();
//Method calls -----------------------------------
avg = average(x, y, z);
small = smallest(x, y, z);
//Method calls -----------------------------------
System.out.println("The average of the numbers is: " + avg);
System.out.println("The smallest of the numbers is: " + small);
}
public static double smallest(double x, double y, double z)
{
double small = 0;
if(z <= y && z <= x)
small = z;
if (x <= z && x <= y)
small = x;
if (y <= z && y <= x)
small = y;
return small;
}
public static double average(double x, double y, double z)
{
return (x + y + z) / 3.0;
}
}
You do need to learn java to do this. See https://en.wikiversity.org/wiki/Java_Tutorial/Hello_World! and others like it for hints.
To answer the question given to you, you only need one class - you have two. In that class, you need three methods. Two of the methods return values - you have them returning a void. You shouldn't need any field variables in your class.
Structure the class as in the tutorial link. (This isnt' a working answer - you still have to do the homework.)
public class Question1 {
public static void main( String[] args ) {
double x, y, z;
// Add your a loop here to collect your x, y and z
// data and process it
while (true) {
// Collect the data ...
// Call the methods
double smallest = smallest(x, y, z);
double average = average(x, y, z);
// Do something with the results
}
}
public static double smallest(double x, double y, double z) {
double smallValue;
// Add your logic and set smallValue
return smallValue;
}
public static double average(double x, double y, double z) {
// Calculate the average and return that value
// as in the smallest method...
}
}
To test the functions, write another program like the following:
import Question1;
public class TestQuestion1 {
public static void main( String[] args ) {
// Add test code here. Something like
double result = Question1.smallest(1.0, 2.0, 3.0)
// Check that the result is 1.0
// Add other tests - output the results
}
Related
So i'm currently trying to finish off my java corse in school but i'm having problem understanding one of the tasks.
The task in question is: Create a method that sums two numbers and returns the sum. The methodhead should look like this; public static double sum(double nr1, double nr2)
and the problem i'm running in to is that i simply do not understand how to solve the task.
The only way of solving this task is with this code.
import java.util.Scanner;
class AddNumbers {
public static void main(String args[]) {
int x, y, z;
System.out.println("Enter two integers to calculate their sum ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = x + y;
System.out.println("Sum of entered integers = "+z);
}
}
But with this solution i'm not solving the task like
This may help you
import java.util.Scanner;
class AddNumbers
{
public static void main(String args[])
{
double x, y, z;
System.out.println("Enter two numbers to calculate their sum ");
Scanner in = new Scanner(System.in);
x = in.nextDouble();
y = in.nextDouble();
z = sum(x,y);
System.out.println("Sum of entered numbers = "+z);
}
public static double sum(double nbr1,double nbr2){
return (nbr1+nbr2);
}
}
Ask the user for 2 sides of a triangle and then print out the length of the third side, as calculated by a method. Write a method to find the length of the third side using the Pythagorean theorem.
I'm new to java and I am suck with the following code which may be way off...
import java.util.*;
public class Tri8 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter two numbers:");
int a = input.nextInt();
int b = input.nextInt();
System.out.println(pythagoraen(a, b));
}
//Method goes here
public static int pythagoraen(int x, int y) {
Math.pow(x, y);
int z = Math.sqrt(x, y);
Math.sqrt(x, y);
}
}
Assuming you meant to find the hypotenuse of a right-angled triangle using the pythagorean theorem, you need to return the root of the sum of squares of both other sides:
public static double pythagoraen(int x, int y) {
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
private static double pythag(double x, double y){
return Math.sqrt(x*x + y*y);
}
or if you want to use math.pow, you can replace x*x with Math.pow(x, 2)
in java calling math.sqrt and math.pow dont directly edit the variables, they actually return a value that you can use, for instance math.pow takes in two double, the first is the base and the second is the exponent, so Math.pow(2,3) would be the same to you and me as 2^3
Here's the fixed code:
import java.util.*;
public class Tri8 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter two numbers:");
int a = input.nextInt();
int b = input.nextInt();
System.out.println(pythagoraen(a, b));
}
//Method goes here
public static int pythagoraen(int x, int y) {
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
}
I'm trying to prompt the user to enter 3 numbers. After those numbers are entered, I am to add the highest two numbers. The main method is to handle all print statements and is to call the other method. I'm not allowed to use for loop for this problem. The variables from the main, should be passed down to the other method.
I am not sure why I am unable to call the method from the main. Here is my code:
public class HW {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Enter three numbers.");
int x = console.nextInt();
int y = console.nextInt();
int z = console.nextInt();
HW.calLargestSum(); //ERROR
HW.calLargestSum(int x, int y, int z); //STILL ERROR
}
public int calLargestSum(int x, int y, int z){
if ( x > y && x > z && y > z )
return x + y;
else if ( y > x && y > z && x > z )
return y + x;
else if ( z > x && z > y && y > x )
return z + y;
return 0;
}
}
You can't call it because you have not instantiated an HW object. Two solutions:
HW hw = new HW();
hw.calLargestSum();
Or make the method static, so that you don't need to instantiate it:
public static int calLargetSum();
Further... ok, so many problems...
HW.calLargestSum(); //ERROR
There is no method calLargestSum(), there is only calLargestSum(int x, int y, int z).
HW.calLargestSum(int x, int y, int z); //STILL ERROR
You need to pass values here. int x is not a value. You need to pass values like:
HW.calLargestSum(1, 2, 3);
Problem
You are making some mistakes when calling the method from main. The non-trivial mistake is that you can't call non-static from static. This happens because if it is not static then it is an instance method. Thus, it requires an instance to access it.
Static Solution
Make your method static. So change your method to:
public static int calLargestSum(int x, int y, int z)
{ ... }
To call the method, you can use:
calLargestSum(1,2,3);
// or in your case.
calLargestSum(x,y,z);
Instance Solution
The other option is to make a new instance of your class (if you don't want it to use static). Like so:
HW hwObj = new HW();
To call use this:
hwObj.calLargestSum(1,2,3);
To See Returned Value/Print
int largest = calLargestSum(x, y, z);
System.out.println(largest);
How can I work with an 'x' variable like in math?
I need to write a code that determines a polynomial function. For example if the input data is 2, 4, 8, 9 then the function would be 2 + 4x + 8x^2 + 9x^3.
Now I do know how to process the input data etc. but I don't know how to declare a variable that has no value, for example I declared a variable x = double, but I still have to initialize it but I don't want to give x a value at this point yet?
So how can I write a method that for example returns 'x' to the power of something?
This is what I have at the moment (it still doesn't work of course)
public class Interpol {
public static void main(String []args) {
Scanner scanner = new Scanner(System.in);
//get user input (polynomial coefficients and interval values x1 and x2)
ArrayList<Double> polynomialCoefficients = new ArrayList<Double>();
int amountCoefficients = 0;
while (scanner.hasNextDouble()) {
polynomialCoefficients.add(scanner.nextDouble());
amountCoefficients++;
}
String in = scanner.next();
double x1 = scanner.nextDouble();
double x2 = scanner.nextDouble();
//call method f to determine the polynomial function
double x;
double[] termsFunction = new double[amountCoefficients];
for (int i = 0; i < polynomialCoefficients.size(); i++) {
termsFunction[i] = f(i, polynomialCoefficients.get(i));
}
//call method findaroot to determine the root
//print result
}
//determine function of which a root is to be found
public static double f(int i, double polynomialCoefficient) {
if (i == 0) {
return polynomialCoefficient;
}
double x;
return polynomialCoefficient * (Math.pow(x, i));
}
/* //rounds off d to 3 decimals
public static double rnd(double d) {
}
//returns a root in this interval, recursive function
public static double findaroot{double x1, double x2) {
}*/
}
You need to make a Class of objects that represent polynomials. The class will store the coefficients in an instance variable, and provide a method to evaluate the polynomial at a given value.
import java.util.*;
public class Foo {
public static void main(String[] args)
throws Exception
{
Scanner scanner = new Scanner(System.in);
//get user input (polynomial coefficients and interval values x1 and x2)
ArrayList<Double> polynomialCoefficients = new ArrayList<Double>();
int amountCoefficients = 0;
while (scanner.hasNextDouble()) {
polynomialCoefficients.add(scanner.nextDouble());
amountCoefficients++;
}
Polynomial polynomial = new Polynomial(polynomialCoefficients);
String in = scanner.next();
while (scanner.hasNextDouble()) {
double x = scanner.nextDouble();
System.out.println("f(" + x + ") = " + polynomial.evaluate(x));
}
}
static class Polynomial {
ArrayList<Double> _coefficients;
public Polynomial(ArrayList<Double> coefficients) {
_coefficients = coefficients;
}
public double evaluate(double x) {
double result = 0;
// This algorithm is called Horner’s rule. See Knuth.
for (int i = _coefficients.size() - 1; i>= 0; i--) {
result *= x;
result += _coefficients.get(i);
}
return result;
}
}
}
If I run this program and type 2 4 8 9 x 2.7, then it prints f(2.7) = 248.267, which is correct according to Wolfram Alpha.
i have initialized a variable value and an inputted value.. netbeans is giving me errors.. can anyone please point out the problem with my code
public class JavaApplication1 {
/**
* #param args the command line arguments
*/
static Scanner sc = new Scanner(System.in);
static double maxLoad = 500;
static double currLoad;
static double loadInput = 0;
public static void main(String[] args) {
String cpNumber;
System.out.println("Enter Cellphone Number");
cpNumber = sc.nextLine();
System.out.println("Enter load to be bought");
loadInput = sc.nextDouble();
computeLoad(maxLoad, loadInput);
System.out.println(currLoad);
}
public static double computeLoad(double x, double y) {
double z = 0;
x - y = z;
return z;
}
}
i got another error. it keeps returning 0..
There are several problems with your code:
1) When you assign a variable, put the variable on the left, and the expression on the right
2) Currently, the return value of computeLoad is ignored. Even when you fix your function to compile, it is not going to work, because currLoad that you print would remain initialized to its default value.
You have two options to fix this:
Change the call to currLoad = computeLoad(maxLoad, loadInput);, or
Change the computeLoad to void, and assign currLoad right there.
it should be:
public static double computeLoad(double x, double y) {
return x- y;
}
x - y = z;
This is wrong. Assignment has to be left to right.
like : z = x - y;
x - y = z; is not a valid Java statement. The assignment operator (=) evaluates the right operand (which may be any kind of expression, method call, literal and soon) and assigns it to the left operand (which has to be an identifier).
z = x - y; would be correct.
You can't do this:
x - y = z
Because assignment works right to left.
z = x - y
Your code should be the following:
public static double computeLoad(double x, double y) {
double z = 0;
z = x - y;
return z;
}