Program Ask For Operator And Two Numbers And Do Math

Feb 17, 2015

This program is supposed to ask for the operator (+ or -) then ask you for two numbers and do the math. When I run it it comes up- Enter operator. When I say add or sub nothing happens. Here it is:

import java.util.Scanner;
public class Echo1{
public static void main(String args[]){
Scanner userInput = new Scanner(System.in);
System.out.println("Enter Operator");
String operator = userInput.next();

[Code] ....

View Replies


ADVERTISEMENT

Find Maximum Between Three Numbers Using Ternary Operator?

Aug 23, 2014

Write a program to find maximum between three numbers using ternary operator.

View Replies View Related

Math On Three Numbers - Get Average / Sum And Number Entered By User

Feb 25, 2015

Java program : For this assignment you are going to write code for the following class:

MathOnThreeNumbers

Here are the specifications of class MathOnThreeNumbers:

Methods of class MathOnThreeNumbers:

1. inputThreeNumbers
2. getAverage
3. getSum
4. getNumberOne
5. getNumberTwo
6. getNumberThree

Constructor of class MathOnThreeNumbers

write a constructor that initializes the first, second, and three numbers to 1, 2, and 3 respectively.

Specs for the methods methods:

1.
name: inputThreeNumbers
accessibilty: public
arguments: none
what it does: asks the user for three numbers of type double

2.
name: getAverage
accessibilty: public
arguments: none
what it does: returns average of the three numbers

3.
name: getSum
accessibilty: public
arguments: none
what it does: returns sum of the three numbers

4.
name: getNumberOne
accessibilty: public
arguments: none
what it does: returns the first number entered by the user

5.
name: getNumberTwo
accessibilty: public
arguments: none
what it does: returns the second number entered by the user

6.
name: getNumberThree
accessibilty: public
arguments: none
what it does: returns the third number entered by the user

Here is an example of how the class MathOnThreeNumbers works. The following code produces the output displayed after the code.

MathOnThreeNumbers mm = new MathOnThreeNumbers();
System.out.println("first: " + mm.getNumberOne());
System.out.println("second: " + mm.getNumberTwo());
System.out.println("third: " + mm.getNumberThree());
mm.inputThreeNumbers();

[Code] .....

View Replies View Related

Remove All Numbers From Queue And Push Onto Stack Until Operator Is Reached - Null Pointer Exception

Apr 26, 2015

This method keeps throwing a NullPointerException. Not only that, but its not doing what I want it to do.

Heres the code:

/*
Remove all the numbers from the queue and push onto the stack until an operator is reached
*/

private static void processQueue() {
// Check what is at the front of the queue. If an operator is found or the queue is empty, exit method.
while(queue.front() != null || !queue.front().equals("*") || !queue.front().equals("/") || !queue.front().equals("%")
|| !queue.front().equals("+") || !queue.front().equals("-"))
{
stack.push(queue.removeFront()); // push the operand onto the stack
}
}

Here is the method that calls the processQueue() method

/*
Process the mathematical expression using a stack and a queue
*/
private static double processExpression() {
// insert all elements from the stack to the queue for processing
while(stack.top() != null) {
queue.insertBack(stack.pop());

[Code] .....

View Replies View Related

How To Restart Math In The Program Without Getting Out Of Program

Feb 15, 2014

I'm working in a GUI program, but I'm not going to put the code because there is a lot of code and files. Instead, I will try to put it an example.

Let say:

I'm working in a GUI program that ask form the user to enter two number's. When the user press at the calculate button. It will show up the output. However, the program won't exit unless the user press at red (X).

int x = user_Input1;
int y = user_Input2;
int total = x + y; //  
JOptionPane.showMessageDialog(null, total);

I know that there will be a (total) now, so my question is here how can I reset all the calculation and have a new total that will show up when the user enter two number's again.

View Replies View Related

When Remove Increment Operator Program Shows Invalid Statement

Feb 19, 2015

class Brain56
{
public static int[] get()
{
return null;
}
public static void main(String...a)
{
int k=1;
get()[k]++;
}
}

when i remove the increment operator(get()[k]) program shows invalid statement may i know the reason behind it?

View Replies View Related

Java Program To Add Two Numbers?

Jan 30, 2014

I want to develop an simple application using java for a simple porgram for the addition of two numbers.

View Replies View Related

Write A Program That Find The Sum Of Two Numbers 62 And 99?

Jan 23, 2015

I have to write a program that find the sum of two numbers 62 and 99 and stores them in a variable named total. However, I have one error that I just can't get rid of and can't tell what it is. I'm using jGrasp and here's what it says:

Programming Challenge #5.java:14: error: class SumofTwoNumbs is public, should be declared in a file named SumofTwoNumbs.java
public class SumofTwoNumbs {
^
1 error
  ----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

and here: is my code:

import java.util.Scanner; 
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
 /**
// This program has variable of several of the integer types.
public class SumofTwoNumbs {
public static void main(String[] args) {

[code]....

View Replies View Related

Program That Use Array To Store 10 Numbers

Oct 16, 2014

i'm trying to write a program that uses an array to store 10 numbers. The numbers should be randomly generated ( Math.random() ), and they should be between 1 and 100 ( 1 and 100 inclusive ). The program should produce an output like the one below:

Element 1 = 23 ( Odd )
Element 2 = 15 ( Odd )
Element 3 = 32 ( Even )
Element 4 - 10 ( Even )
Element 5 - 99 ( Odd )
Element 6 - 1 ( Odd )

[Code]...

I have written code for this but its only showing me 0's after first number can you check whats wrong with my code. my code is.

class even/odd{
public static void main(String[]args){
int y=0;
int z=0;
int[] array= new int[11];
for(int x=1; x <array.length ; x++){
array[x]= (int) (Math.random()* 100);
 
[Code]...

View Replies View Related

Program To Find If Numbers Are Consecutive Or Not

Feb 23, 2014

im trying to do a program to find if numbers are consecutive or not! if they are consecutive i need a true as a return and a false when they are not... i did this program and im sure i did something wrong because i keep only true returns ..

Example: (3,5,7) (1,2,2) (7,7,9) should return a false!
Java Code: import java.util.*;
public class Consecutive{
public static void main (String [] args){
Scanner console= new Scanner(System.in);
System.out.println("Enter three numbers");
String numbers = console.nextLine();
System.out.println( "The numbers (" + numbers + ") is '" + consecutive( numbers ) + "'" );
}//end of main

[code]...

View Replies View Related

Compound Assignment Operator

Jan 8, 2014

I just cant seem to understand the order of precedence here.

class Test{    
public static void main(String[] args){       
int k = 1;             
k += (k = 4) * (k + 2);       
System.out.println( k );    
}
}

From what I have read compound operators have the lowest order of precedence... But the above piece of code makes the assignment k = 1 + (k = 4) * (k + 2) before evaluating the rest of the statement first.

It then evaluates (k = 4) and proceeds with the remained of the statement 1 + 4 * (4 + 6)....

I dont understand why the first k is assigned 1 but the remaining ks 4. Should they not all be 1 or 4 (I would have thought 4, since += has the lost order of precedence so is evaluated last)??

View Replies View Related

Difference Between Operator And Method

Jun 4, 2014

I know instanceof is an operator and println is a method. But what is the difference between the two? How are they different/same?

View Replies View Related

Operator In Case Of Strings

May 23, 2015

After the executing the code below:-

class Test
{
public static void main(String [] args)
{
String s = new String("new") ;
String d = new String("new") ;
System.out.println((s==d)+ " "+s.equals(d)) ;
System.out.println(s==d + " "+ s.equals(d)) ;
}
}
OUTPUT:- false true
false

Why did the output change in the second print statement?

View Replies View Related

Calculating Sin(x) Without Using Math

Sep 14, 2014

I'm trying to calculate sin(x) without using Math.sin(x). The formula for sin(x) is: x - x^3/3! + x^5/5! ... I can't seem to get the coding for the alternating +/- right. Here's my program:

import java.util.Scanner;
import java.lang.Math;
class Sin
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
int n, c, fact = 1, count = 1;
double x, sum = 0, sum_sin = 0, result;

[Code] ....

View Replies View Related

Write A Program That Takes Two Numbers And Multiplies Them

Jul 8, 2014

I am trying to write a program that takes two numbers and multiplies them. I got this idea from this running conversion program

import java.util.Scanner;
public class Convert
{
public static void main (String [] args)
{
Scanner reader = new Scanner (System.in);
double farenheit;
double celsius;

[code]...

i am getting a cant find symbol for the output = (alpha*beta) line..Also, am I using the reader object correctly ? hould I create two reader objects as there are 2 inputs needed?

View Replies View Related

Program That Displays First 100 Pentagonal Numbers Using Different Methods

Feb 28, 2015

I have to write a program that displays the first 100 pentagonal numbers using different methods. This is what I have so far:

Java Code:

public class FivePointOne{
public static void main(String[]args){
System.out.println("The the pentagonal numbers are: ");
}
//this method will find the penagonal numbers
public static int getPentagonalNumber(int n){

[Code] ....

But when I compile it to test it out I receive the error message "Illegal start of expression," ';' expected, and "reach end of file while parsing".

View Replies View Related

Developing A Program That Inputs Telephone Numbers

Jan 26, 2014

Develop an algorithm for a Java program that takes as input a single letter and displays the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way:

2 = ABC 3 = DEF 4 = GHI 5 = JK 6 = MNO 7 = PQRS 8 = TUV 9 = WXYZ

The screen dialog might look like this: Enter a single letter, and I will tell you what the corresponding digit is on the telephone. R The digit 7 corresponds to the letter R on the telephone.

The program should display a message indicating that there is no matching digit for any non-alphabetic character entered by the user. Also, the application should recognize only uppercase letters. If a user enters a lowercase letter, the program should display an error message. Prompt the user with an informative message for the input value as shown above.

The application should include the input letter as part of the output.

Write the algorithm to solve this problem including steps for prompting the user, getting input from the keyboard, and displaying the results.

Write the Java program that you designed, using your algorithm as a guide. Use your algorithm as comments in the body of the main method.

Use proper indentation, and meaningful identifiers throughout the code.

View Replies View Related

Program Should Print Out Numbers For Each Vehicle Category

Aug 2, 2014

2.Check the Vehicle...Assume that vehicles are going through a two-way traffic intersection. There are three types of vehicles: car, motor bikes and trucks. Generate a series of 10 random integers, between 1 and 3, inclusive. The numbers represent the type of vehicle as stated below:

NumberVehicle Category
1 Car
2 Motor bikes
3 Trucks

Write a program, using a for loop, to count how many vehicles going through the traffic intersection are cars, motor bikes and trucks. Then, the program should print out the numbers for each vehicle category. There is no user input for this program.

View Replies View Related

Order Of Numbers - Reverse Array Program

Apr 23, 2015

import java.util.Scanner;

public class ReverseOrder {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
int[] numbers;
double[] reverse;

numbers = readArray();
reverse = reverseOrder(numbers);

[Code] ....

I have to write a program that takes an order of numbers and print the reverse order of those numbers.

the out put i receive is:

How many numbers do you want to enter?
4
Please enter 4 integers separated by a space:
1 2 3 4
Your numbers in reverse order are:[D@1f96302

View Replies View Related

Why Operator Overloading Is Not Supported In Java

Oct 18, 2014

why operator overloading is not supported in Java.

View Replies View Related

Using Values That Are Set For Cases In The Switch Operator

Mar 23, 2014

I am new to Java and I am trying to use values that are set for cases in the switch operator.

The first menu ask for you to pick a product and each product has a price on it.

The second menu ask you to pick a state with each state having a decimal value on it.

The third is asking you to put the number of cases and each case is 12 items.

A key note to remember is that each part that a person is choosing is on a different instance!

Here is an example of what i am trying to do.

Menu 1: I picked case 1 that is Computer and it is worth 1000
Menu 2: I picked case 1 that is CT and it's tax is 7.5

Third choice: I picked case 1 and that has 12 items

I want the subtotal witch is: (1000 * 12)

Subtotal in this situation is: 120000

Next i need the total value which is based on what state they picked for the tax percent value picked from the state menu case: (12000 * 0.075 + 120000)

Total value is: 129000

I will post the code I have but based on the choices a person makes will determine the values and I need those values set in the cases to put in a math equation. The problem I am having is retrieving these numbers form the cases inside the menu options and they are on a different instance. So How can I do this in Java code?

Here is the code:

This is menu 1

Java Code:

import java.util.*; //scanners and more
class menu{
public void display_menu() {
System.out.println ("Please select your product"); //Gives user direction
System.out.println ( "1)

[Code] .....

View Replies View Related

How To Type Bitwise Operator Symbol

Jul 6, 2014

It looks like an upside down v , i cant find it on my keyboard. I assume its a keyboard combo.

View Replies View Related

HashMaps / ArrayLists - Diamond Operator?

Jul 8, 2014

So I am learning HashMaps/Arraylists and I can't really understand the diamond operator or what it's for. Take the following code for example: Why could we not just do this without the diamond?

Java Code:

import java.util.HashMap;
class Untitled {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<String, String>();

map.put("California","Sacramento");
map.put("Oregon","Salem");
map.put("Washington","Olympia");

System.out.println(map);
}
} mh_sh_highlight_all('java');

View Replies View Related

Bad Operand Types For Addition Operator

Apr 25, 2015

I would like to know that i am getting an error with the random. it is say bad operand types for binary operator ' +'

[/ Random generator = new Random(15) + 1];

View Replies View Related

And And Or Operator - Manipulate Bits In A Variable

Jun 1, 2014

I am trying to understand what ivor is saying about the and, and or operators and the mask. If I understand it correctly the & operator prevents you from changing a bit that is one when a mask is involved and changes all others to 0 and the | operator forces a bit to 1 when the mask is 1.

My question is when would i need to actually use the & ,| operators ?when will i need to manipulate the bits in a variable?

View Replies View Related

Split A Math Expression?

Feb 27, 2015

I have a math expression in a String, that I want to split into 4 pieces as in the following example:

String math = "23+4=27"
int num1 = (23 STORES HERE)
int num2 = (4 STORES HERE)
String operator = (+ STORES HERE)
int answer = (27 STORES HERE)

Note that the operator can be either + - * /

View Replies View Related







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