5571 Shares

How do these simple math equations in a java program?

How do these simple math equations in a java program? Topic: How to write absolute value in java
July 16, 2019 / By Lenora
Question: Write a program that prompts the user for two numbers, then prints The sum The difference The product The average The distance (absolute value of the difference) The maximum (the larger of the two) The minimum (the smaller of the two) To do so, implement a class public class Pair { /** Constructs a pair. @param aFirst the first value of the pair @param aSecond the second value of the pair */ public Pair(double aFirst, double aSecond) { . . . } /** Computes the sum of the values of this pair. @return the sum of the first and second values */ public double getSum() { . . . } . . . } Then implement a class PairTester that constructs a Pair object, invokes its methods, and prints the results. Your tester class should be called PairTester. You need to supply the following class in your solution: Pair
Best Answer

Best Answers: How do these simple math equations in a java program?

Jonquil Jonquil | 9 days ago
Let's assume your two parameters are 'a' and 'b'. The sum a+b The difference a-b The product a*b The average a+b/2 The distance (absolute value of the difference) If you don't want to learn how to do absolute value, you can do this cheesey way: if( a > b ) { result = a-b; } else { result = b-a; } The maximum (the larger of the two) if( a > b ) { return a; } else { return b; } The minimum (the smaller of the two) if( a < b ) { return a; } else { return b; }
👍 126 | 👎 9
Did you like the answer? How do these simple math equations in a java program? Share with your friends

We found more questions related to the topic: How to write absolute value in java


Jonquil Originally Answered: Simple Java program using interface which can't be solved using abstract class?
The idea of using an interface compared with a class is to solve the problem of only single inheritance in Java. By that I mean, Java only allows you to extend (or inherit from) a single class. If you want methods that can be used by different classes that are not related to each other, then you would use an interface so the classes can have the method. Here is an example of that. Let's say you have a number of animals and insects. Since they come from different species, they should not all extend from a base class. That is, an insect is not an animal, and an animal is not an insect. But, they all need to eat. So you could create an interface that has a method called eat. Then all animals and insects could implement the interface and have a method eat so they can all eat. Hope that helps.
Jonquil Originally Answered: Simple Java program using interface which can't be solved using abstract class?
It is very sad that your professor has taught you *how* to use these tools but not *why* to use these tools. To be honest, knowing the "how part" is practically worthless. Anybody can pick up a book or google for syntax and know the how. If you obtain a career as a programmer, you will learn syntax and rules again and again.. they are largely unimportant. The reason we pursue education from professionals is to learn why these concepts exist at all, where there provide a benefit, when they are appropriate and when they are not. Maybe your professor is trying to teach the other, more important stuff and you just aren't getting it for whatever reason, but it sounds like the focus of his instruction is not very good. If this is typical of this professor, I would transfer to another instructor if at all possible. And as for your original question... you're basically asking for knowledge that comes from hours of study, practice and refinement. (something most students get in a typical CS class, but apparently you did not). This is not something to be addressed in a simple yahoo answer.
Jonquil Originally Answered: Simple Java program using interface which can't be solved using abstract class?
interface IMovable { update(); } class LeadBalloon implements IMovable { int locx; int locy; int vx; int vy; public LeadBalloon() { locx = 3; locy = Integer.MAX_VALUE - 1; vx = 1; vy = 100000000 * -1; } public update() { locx +=vx; locy += vy; } } //>================= class RocketScience { static void main( String [] args ) { LeadBalloon[ ] racers = new LeadBalloon[ 3 ]; racers[ 0 ] = new LeadBalloon(); racers[ 1 ] = new LeadBalloon(); racers[ 2 ] = new LeadBalloon(); for( int i = 0; i< racers.length; i++) { racers[ i ].update(); }

Genista Genista
you need to use the gadgets to speak between the educational:- type Operation { inner maximum char operator; void getOperator(void) { //as consumer to go into an oprator and assign it to operator variable; } int performOperation(int n1,int n2) { change(operator) { case '*':return n1*n2; ruin; case '+':return n1+n2; ruin; ......... ....... default: ruin; } } } } type Calculator { inner maximum in n1,n2; int getN1() { return n1; } int getN2() { return n2;; } void getNumbers(int n1,int n2) { gadget.out.println("enter any 2 numbers:") //test numbers in variables n1 and n2 } } public type mainCalci { public static void substantial(String args[]) { Operation object1=new Operation(); Calculator object2=new Calculator();//now substantial type has gadgets of Calculator and Operator type which may be utilized to call the strategies of the two training. object2.getNumbers(); object1.getOperator(); int consequence=object1.performOperation(obj... } }
👍 40 | 👎 6

Genista Originally Answered: If I write a program using Java in Eclipse how can I then run the program from the internet?
It's as easy as just adding in the code :) If you've written a Swing application, it's the same basic idea. It's a little odd to work with if you haven't though. I personally try to avoid it, but I do have some experience with it, so you can email me if you like. Though this tutorial should help you out with every thing: http://docs.oracle.com/javase/tutorial/d...
Genista Originally Answered: If I write a program using Java in Eclipse how can I then run the program from the internet?
Yes you need to make an applet. It's not too difficult, just a modified version of your driver/ main method. The second part is that you need to make an html file to launch you applet. You then click on the html file and your java program will automatically launch in a web browser. But.. based on the fact that you're a woman you should just get back to the kitchen

If you have your own answer to the question how to write absolute value in java, then you can write your own version, using the form below for an extended answer.