How To Compute All Possible Arithmetic Operations Of 4 Integers

Nov 25, 2014

I'm new to Java and I want to write a program that compute all the possible arithmetic operations of 4 integers.

For example, if the user enter 1, 2, 3, and 4 respectively, then I want the computer to print out the following results:

1 + 2 + 3 + 4
1 + 2 + 3 - 4
1 + 2 + 3 * 4
1 + 2 + 3 / 4
..
1 / 2 / 3 / 4

(parenthesis can be ignored for this program)

This is what I have so far, but I don't know how to continue:

import java.util.Scanner;
public class ArithmeticComputation{
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int num1 = stdin.nextInt();

[Code] ....

I'm looking for a method that allows me to assign char into actual operators, so that I can loop through all the computation.

View Replies


ADVERTISEMENT

Simulate Simple Calculator That Performs Basic Arithmetic Operations Of JAVA

Sep 9, 2014

How can i write a java program that simulate a simple calculator that performs the basic arithmetic operations of JAVA. (JOption Pane and Repeat control structure)

Example : Addition, Subtraction, Multiplication, Division.

The program will prompt to input two floating point numbers and a character that represents the operations presented above. In the event that the operator input is valid, prompt the user to input the operator again.

Sample Output :

First number 7
Second number 4
Menu

<+> Addition
<-> Subtraction
<*> Multiplication
</> Division

Enter Choice: * <enter>

The answer is 28.

View Replies View Related

Compute Average Of The Sum

Dec 30, 2014

I'm having trouble compiling this program. It is supposed to compute the average of the sum:

import java.util.Scanner;
public class ComputeAverage
{
public static void main(String[] args) {
Scanner bucky = new Scanner(System.in);
double fnum;
double snum;

[Code] ......

View Replies View Related

Java - Compute PI Using Equation

Oct 24, 2014

Part 1: USING A WHILE OR A DO-WHILE LOOP write a program to compute PI using the following equation:

PI = 3 + 4/(2*3*4) - 4/(4*5*6) + 4/(6*7*8) - 4/(8*9*10) + ...

Allow the user to specify the number of terms (5 terms are shown) to use in the computation. Each time around the loop only one extra term should be added to the estimate for PI.

Alter your solution from part one so that the user is allowed to specify the precision required between 1 and 8 digits (i.e. the number of digits which are correct; e.g. to 5 digits PI is 3.14159), rather than the number of terms. The condition on the loop should be altered so that it continues until the required precision is obtained. Note that you need only submit this second version of the program (assuming you have it working).

My output for part 1 is :

Enter number of wanted terms for value of PI
100
PI = 3.1666666666666665 Number of Term = 2
PI = 3.1333333333333333 Number of Term = 3
PI = 3.145238095238095 Number of Term = 4
PI = 3.1396825396825396 Number of Term = 5
PI = 3.1427128427128426 Number of Term = 6
PI = 3.1408813408813407 Number of Term = 7
PI = 3.142071817071817 Number of Term = 8
PI = 3.1412548236077646 Number of Term = 9
PI = 3.141839618929402 Number of Term = 10 ................ and so on

I know you have to compare value of current Pi to the previous one but I'm stuck.

View Replies View Related

How To Count Primitive Operations

May 2, 2014

I have having some trouble on counting the primitive operations on the pseudocode given below:

Algorithm 4. MaximumArray(Arr)
Input: A 1-D numerical array Arr of size n
1) Let CurrentMax = a0
2) For i = 1 to n-1
3)If ai > CurrentMax Then CurrentMax = ai
4) End For
Output: CurrentMax, the largest value in Arr

As of now, I know that for Line 1 there are 2 operations (one set and one read). I don't know how to figure out the for loop and If statement (line 2 and line 3 too).

View Replies View Related

Nested Loops Or Set Operations?

Oct 10, 2013

Is there any advantage to using Java's operations defined in sets or collections over inspecting in nested for-loops?

For example, suppose I want to keep items from one array that do not appear in another, I could do:

for( PrimitiveType prim_1 : someArray ){
for( PrimitiveType prim_2 : otherArray ){
if( prim_1 != prim_2){
///put in some return list
}
}
}

Or else I could do:

ArrayList<NotPrimitive> excludedElements =
new ArrayList(Arrays.asList(primitiveArrayToExclude));
for( PrimitiveType elem : someArray){
if( ! excludedElements.contains( elem ) {
//put in some return list
}
}

I personally see no improvement in legibility nor any reduction of error potential.....

View Replies View Related

I/O / Streams :: Any Way To Parallel I/O Operations

Jul 31, 2014

I've been wondering about this for a while. Is there any way to parallel I/O operations or is this a bad idea? If you could create two lists of good and bad ways to parallelize I/O.

View Replies View Related

Using A Menu For Stack Operations

Dec 3, 2014

I am working on implementing a stack using a linked list. Programming the stack operations (push, pop, isEmpty, etc.) was easy but my directions were to provide the user with a menu to choose the operation that he/she wishes to perform. I am new to JFrames/Menus so how to make the stack operations available in a menu.

View Replies View Related

Illegal Start Of Operations For All Methods

Apr 21, 2015

Where are all these illegal start of operations and all of these other method errors coming from.I've written enough methods not to have this problem.

----jGRASP exec: javac -g Assessement.java
Assessement.java:29: error: illegal start of expression
public void addQuestionAnswer(){
^
Assessement.java:29: error: illegal start of expression

[code].....

View Replies View Related

LISP Expression - Performing Operations

Jan 29, 2015

Given a LISP expression, perform operations on the expression. There will be no list elements that also contain a list such as '(A (B (C D))), which has one atom and one list but the list contains a sublist

INPUT: There will be 5 lines of input. Each line will contain a valid LISP expression. There will be a space between each atom and each list. There are no spaces immediately after or immediately before parentheses in a list. The entire expression must be inputted as a single string.

OUPUT: Perform the given operation on the like numbered expression. The 5 operations are:

1. Print the expression with the list in reverse order. The list will contain only atoms.

2. Print the expression with the list written with consecutive duplicates encoded as sublists in (count element) order. The list will contain only atoms.

3 Print the expression with the list written with consecutive duplicates encoded as sublists in (count element) order except that singletons are listed as atoms. The list will contain only atoms.

4. Print the expression with the list written with every Nth element deleted and where N is the last element of the list.

5. Print the expression written as 2 expressions where the number of lists in first expression is the last element of the expression.

SAMPLE INPUT SAMPLE OUTPUT
1. '(A B C D) 1. ′(D C B A)
2. '(A A A A B C C A A D E E E E) 2. ′((4 A) (1 B) (2 C) (2 A) (1 D) (4 E))
3. '(A A A A B C C A A D E E E E) 3. ′((4 A) B (2 C) (2 A) D (4 E))
4. '((4 A) (1 B) (2 C) (2 A) (1 D) (4 E) 2) 4. ′((4 A) (2 C) (1 D) 2)
5. '((4 A) (1 B) (2 C) (2 A) (1 D) (4 E) 3) 5. ′((4 A) (1 B) (2 C)) ′((2 A) (1 D) (4 E) 3)

View Replies View Related

Scientific Method - Compute Base Times 10 To Exponent

Feb 13, 2015

This is what I need to do: Write a method called scientific that accepts two real numbers as parameters for a base and an exponent and computes the base times 10 to the exponent, as seen in scientific notation. For example, the call of scientific(6.23, 5.0) would return 623000.0 and the call of scientific(1.9, -2.0) would return 0.019.

This is the method I have written:

public double scientific(double num1, double num2); {
double base = num1;
double exp = num2;
double scientific = base * Math.pow(10, exp);
System.out.println(scientific);
return scientific;
}

These are the following error messages I received when trying to compile the code:

Line 4
missing method body, or declare abstract
missing method body, or declare abstract
public double scientific(double num1, double num2);

[Code] .....

How to fix each error?

View Replies View Related

Postfix Calculator Doesn't Compute Negative Numbers

Nov 16, 2014

My verify method also always returns false. So I'm given three classes to begin with. Calculator, Expression, and InfixExpression and they are listed below.

The goal is to create a class called PostfixExpression that extends Expression and can read and calculate postfix expressions.

My evaluate() method works for most calculations but when it needs to return a negative value it just returns the positive equivalent.

Also, my verify method always returns false and I can't pinpoint why.

Here's my current code. Some things are commented out for debugging purposes.

import java.util.Scanner;
/**
* Simple calculator that reads infix expressions and evaluates them.
*/
public class Calculator
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

[Code] .....

View Replies View Related

Confirming Conversion To Compute - Calculate Fahrenheit Or Celsius Value

Sep 28, 2014

<import javax.swing.JOptionPane;
public class TempCalc {

public static void main(String[] args) {

//Confirming which conversion to compute.
String FarOrCel = (JOptionPane.showInputDialog("Would you like to calculate a Farenheit value or a Celsius value? (Enter Farenheit or Celsius)");

[Code] ....

So I've been trying to come up with a Farenheit to Celsius converter and back. However, whenever I run the program in JCreator it never recognizes the string "Farenheit". Even if I put that exact thing (I copy/pasted from the if (FarOrCel == "Farenheit")) it keeps asking me for a Celsius value.

[quote name='INNOCENTREAPER##!' date='28 September 2014 - 07:40 PM' timestamp='1411958437' post='2057072']

<import javax.swing.JOptionPane;
public class TempCalc {
public static void main(String[] args) {

//Confirming which conversion to compute.
String FarOrCel = JOptionPane.showInputDialog("Would you like to calculate a Farenheit value or a Celsius value? (Enter Farenheit or Celsius)";

[Code] ....

View Replies View Related

Compute Standard Deviation For A Set Of Numbers - ClassCastException Being Thrown?

Apr 20, 2015

Basically I'm trying to compute the standard deviation for a set of numbers and I'm using previously know data to compute this. The data I'm working with is here (note all of this is fake as its for an assignment):

Id,First Name,Last Name,Quiz1,Quiz2,Quiz3,Lab1,Lab2,Assignment1,Exam1,Exam2
58136,Wade,Andrews,5,8,2,2,20,47,20,40
59759,Jacqueline,Simmons,9,4,1,16,13,2,82,41
28056,Frederick,Castro,8,0,6,3,6,29,53,63
15532,Annette,Myers,2,6,0,14,13,4,77,43

Next this is the code that I know is producing the error:

/**
* Pass in original data from files, and pass in computed grades as arraylist. this returns all of the standard deviations to be printed
*/
public static ArrayList calcStandDev(String data1[], ArrayList<Double> grades, ArrayList<Integer> averages)
{
double standDev = 0;
int j = 0;
//gets rid of first element in string array, makes processing MUCH easier
String[] data = Arrays.copyOfRange(data1, 1, data1.length);
 
[Code] ....

Look for the line with all the ***'s, it is producing the "java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Integer".

The reason I dont undersand why its producing this error is because temp.get(i) should be a integer - averages.get(i) (is type double), I do believe automatic up casting is present here making them both a double then I multiply it by itself producing a double? I dont understand where the casing is occurring. And just incase you want the formula I'm using for standard deviation here it is:

((x1 - ave)^2 + (x2 - ave) ^2 + (xn - ave) ^ 2) / (n-1)

then squareroot above

Why I'm getting this error! I tried changing a few dataTypes but I just cant avoid exceptions being thrown everywhere .

View Replies View Related

Unchecked Or Unsafe Operations While Compiling Program?

Jun 30, 2014

String filename="C:UsersRajashekarDesktopfwfwdSoftware Failures1_Test.txt";//Input Files
String data;
public ArrayList<String> value=new ArrayList<String>();
public void read() throws IOException{
File f = new File(filename);

[Code] ....

View Replies View Related

What Are Interleaving Operations - Can Atomic Actions Interleave

Mar 24, 2014

I have read that when two threads have two steps (of execution) and each step involves many operations, then the operations of both the threads kind of overlap one-another. For example

I think I am not very clear about interleaving actions/operations? Especially when we say that atomic actions can not be interleaved. But

Java Code:

class Counter{
private int c=0;
void increment () {
c++;
}
void readIn () {

[Code] ....

And the following sequence of events occurs.

Thread A: Retrieve cThread A: Increment cThread A: Store the result in cThread B: Ask the user to enter a valueThread B: Read in the value, store it in c

Now the value of c obtained after increment will be lost (Thread Interference). Isn't this an example of interleaving operations?

View Replies View Related

JSP Calculator - Performing Basic Operations On Numbers

Jul 8, 2014

I want a simple jsp page that performs basic operations on numbers such as addition, subtraction, multiplication, division and modulus . How to run that file?

View Replies View Related

Queue Operations In Java - Using Reference As Pointer

May 28, 2014

I am trying to run the queue operations in java such as enqueue, dequeue using pointers ... How to use references as pointer in java.

View Replies View Related

JavaFX 2.0 :: Perform Boolean Operations On 3D Primitives?

Nov 10, 2014

How to perform boolean operations (union, intersect and subtract) on 3D primitives in javafx 3D?

View Replies View Related

Program That Compute Cost Of Painting And Installing Carpet In A Room

Jan 30, 2015

I need to create a code for this asign:

Write a program that computes the cost of painting and installing carpet in a room. Assume that the room has one door, two windows, and one bookshelf. Your program must do the following:a. Prompts the user to enter, in feet, the length, width, and height of a room.

A. Read the dimensions of the room.

b. Prompts the user to enter the width and heights, in feet, of the door, each window, and the bookshelf. Read these quantities.

c. Prompts the user to enter the cost, per square foot, of painting the walls. Read these quantities.

d. Prompts the user to enter of cost, per square foot, of installing carpet. Read these quantities.

e. Output the cost of painting the walls and installing the carpet.

View Replies View Related

Java Code To Determine If It Is Valid Triangle And Then Compute Area

Apr 4, 2015

I need to create a program in Java for below question

"Prompt user for three sides of triangle and determine if it is valid triangle and then compute area"

With code and Sudo Code for above question.

I am using Netbean compiler.

View Replies View Related

Arithmetic Tester Program

Aug 6, 2014

How do i go about developing a simple arithmetic tester program which can be used to support young children improve their arithmetic skill. The program should accepts string data for the player's name and the number of questions they want to try, which will be entered by the user. The program should take in the following inputs from the user:

The name of the player, so that the game can refer to them by name in messages. The name must be between 2 and 20 characters inclusive.

The number of sums that the user wants to attempt. This number must be a whole number, and be between 2 and 50 inclusive.

The program must check that the values entered fall within the valid ranges. The program must also check that no empty strings are entered for names.

Once the player has entered their name and the number of questions they want to attempt, the game begins. The program must generate sums for the player.

The program should generate one question at a time and display the question for the user. The questions must involve two numbers and be either a multiplication, division, subtraction, or addition. For example, the following are all valid questions:

3 * 4 = ?
8 / 2 = ?
7 + 1 = ?
9 - 8 = ?

The first number, second number, and arithmetic operator (multiply, divide, add, subtract) should all be chosen randomly, the program should Generate a new random number for each of the three parts of each sum. this requires you to choose one of the four symbols based on which random number is returned.

A few checks must be carried out before a generated question is displayed to the user. If the question is an addition question, no checks are required. If the question is a multiplication question, no checks are required. However, checks are required for subtraction and division sums.

If the question is a subtraction question, the program must check that the second number is equal to or smaller than the first number. Whilst the second number is larger than the first number, new numbers should be generated.

View Replies View Related

How To Develop Complete Arithmetic Class

Nov 21, 2014

 import java.util.*;
 public class TestArithmetic {
  public static void main(String [] args) {
  Scanner console = new Scanner (System.in);
  System.out.println("Enter First Number");
int x1 = console.nextInt();
System.out.println("Enter Second Number");

[code]...

View Replies View Related

Arithmetic Expression With Quadratic Formula

Oct 3, 2014

I have just started to learn programming in the book I am teaching myself from it said how would you write the following arithmetic expression with it being the quadratic formula but only the plus part of it in the plus or minus..

package javalearning;
import java.util.Scanner;
public class QuadraticFormula {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter value for B: ");
double B = input.nextDouble();
System.out.print("Enter value for A: ");
double A = input.nextDouble();
System.out.print("Enter value for C: ");
double C = input.nextDouble();
double negativeOfB = -B;

[code]....

View Replies View Related

Swing/AWT/SWT :: Else If Statements - Arithmetic Sequence

Apr 1, 2014

So I have one set of If Else If statements. My program calculates just the first comboBox. It is retrieving all the values from the other comboBoxes but I do not understand where to place the other If statement structure so I can calculate the other values I have set for the other objects. I would not like to use a different class for each and I know I have my calculations done right below my current if statement. Could it be left there and where shall I place the other if statements so they get read by my arithmetic sequence?

public void actionPerformed(ActionEvent e)
{
Object source = e.getSource() ;
int deckSiz = (int) Decks.getSelectedIndex() ;
int wheelSiz = (int)Wheels.getSelectedIndex();

[code]....

View Replies View Related

Catch Arithmetic Exception In Program

Jan 29, 2015

I want to catch the exception in my program is the below way:

class Temp
{
public static void main(String s[])
{
try
{
int x=10/s.length;
System.out.println(x);

[Code] ....

I am expecting my program should give output as "java.lang.ArithmeticException: / by zero" but is giving

Temp.java:11: error: ')' expected
catch (ArithmeticException e | ArrayIndexOutOfBoundsException e)
^
Temp.java:11: error: ';' expected
catch (ArithmeticException e | ArrayIndexOutOfBoundsException e)
^
Temp.java:16: error: 'catch' without 'try'
catch (Exception e)
^
Temp.java:22: error: reached end of file while parsing
}
^
4 error

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved