Int And Double - Performing Floating Point Arithmetic
Sep 19, 2014
I am new Java Programming and I am struggling to pass my Java class. How to perform Java but I am trying. For this particular assignment I supposed to:
* Change all variables' data types to double.
* Change the two prompts to request double values
* Change change the two calls to the nextInt() method to nextDouble().
This is the original assignment:
import java.util.Scanner;
public class ArithmeticDemo
{
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
int firstNumber;
int secondNumber;
int sum;
int difference;
int average;
how the data is stored in float. It seems like the range would be greater because it stores scientific notation rather than plain value, whilst integer arithmetic performance is better. float should be used to store bigger values and integer should be used for speed when values are smaller. As an example, I want to have cubic volumes ranging from about a handful to cargo ship. So float would be necessary for that.
Develop the class “Account” to be used by a bank. The Account class should have a single instance variable “balance”. Remember that balance must be a floating point number. The required methods for the Account class are shown below.
The Account class “debit” method should return a Boolean and should not allow an overdraft. If a withdraw greater than the current balance is attempted, the function should immediately return “false” and do nothing else.
Develop a test class to thoroughly test all aspects of the Account class. DO NOT change the class name or instance variable name given or the required method names as detailed below.
Account 1 constructor with no parameters (default balance to 0) 1 constructor with a balance parameter setBalance method getBalance method credit method debit method
I have the test class done, I won't need to put that in till later. My main problem is I'm not sure how I'm going to be able to get debit and setBalance to work together with each other.
public class Account { protected double balance; // Constructor to initialize balance public Account( double amount ) { balance = amount;
[Code] ....
You can see I'm stressed out by not reading over my code. I already have the "Debit" in use, just have to change it. Which I did.
Write a program OutCircle.java that declares and initializes three floating-point variables (r, x, y): the first variable represents the radius r of a circle centered at (0,0) and the second and third variables are the coordinates (x, y) of a point in the plane.Your program should print true if the point is outside the circle and false otherwise. Hint: A point is outside the circle when its distance to the center is greater than the radius.
I am trying to do this assignment but I can't get the needed output.
Create a program that asks the user how many floating point numbers he wants to give. After this the program asks the numbers, stores them in an array and prints the contents of the array in reverse order.
Program is written to a class called ReverseNumbers.
Example output
How many floating point numbers do you want to type: 5
Type in 1. number: 5,4 Type in 2. number: 6 Type in 3. number: 7,2 Type in 4. number: -5 Type in 5. number: 2
Given numbers in reverse order:
2.0 -5.0 7.2 6.0 5.4
Java Code:
import java.util.Scanner; public class apples { public static void main(String[] args) { Scanner reader = new Scanner(System.in); double[] numbers;
This may be a multipart question. Basically I have to write a program that accepts a string, converts it to a double splits it and then performs a calculation.
public static void main(String[] args) { String input = ""; // initalize the string boolean isOn = true; // used for the while loop...when false, the program will exit. String exitCommand = "Exit"; // exit command String[] token = input.split(("(?<=[-+*/])|(?=[-+*/])"));
[Code] ....
This is the error I get:
Enter a math problemException in thread "main" java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1842) at sun.misc.FloatingDecimal.parseDouble(FloatingDecim al.java:110) at java.lang.Double.parseDouble(Double.java:538) at Calculator.main(Calculator.java:22)
[Code] .....
How do I go about actually fixing it. Without that line commented out I get this:
Enter a math problem 2+2 <-What I entered [Ljava.lang.String;@135fbaa4
Also, how would I go about using the submethods to check for valid operands and operators (also part of the problem) I have the actual lists made obviously, but am unsure how to get those to work with the problem.
I've just been having a go at an exercise where I have to create and use a class called Point, with two fields of type double. I have to create some methods for it, one of which is a distanceTo(Point) method, that calculates the distance to another point. I've tried to keep the distanceTo(Point) method short so have created some other methods to use within the method. My question is about the getDistance() method that I've made. As you can see below, I've given it two parameters, which are references to values within two Point objects (this.x and otherPoint.x).
I was reading the oracle java tutorial under: URL....Here's the code for the Point class:
public class Point { public int x = 0; public int y = 0; //constructor public Point(int a, int b) { x = a; y = b; } }
and in the Rectangle class you have the following constructor:
public Rectangle(Point p, int w, int h) { origin = p; width = w; height = h;
If we create a new Point object like this:
Point originOne = new Point(23, 94);
and then a new Rectangle object like this:
Rectangle rectOne = new Rectangle(originOne, 100, 200);
Will that set originOne to point to the object Point at (23, 94). just want to make that this is the meaning of this statement: Point(Point p)Constructs and initializes a point with the same location as the specified Point object.
I have some problems about performing regular expression. In the following code, the output as a result is being "valid last name". But in my opinion, it should be "invalid last name". Because "Doe" has not any space, apostroph or any hypen. Look at the regular expression statement: "[a-zA-Z]+([ '-][a-zA-Z]+)*"
package deneme; public class Deneme { public static void main(String[] args) { String lastName = "Mc-Something"; // Mc-Something if(!lastName.matches("[a-zA-Z]+([ '-][a-zA-Z]+)*")) System.out.println("Invalid last name"); else System.out.println("Valid last name"); } }
I know that the asterisk qualifier means character(s) in any number.(Is this wrong?) So for "Mc-Something", 'M' corresponds to [a-zA-Z], 'c' corresponds to +, - corresponds to [ '-], 'S' corresponds to [a-zA-Z], "o" corresponds to +, and finally "mething" corresponds to *, doesn't they? But for "Doe", there is no any space, apostroph or hypen.
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)
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?
importjava.util.Scanner; public class ManageAccounts {
[Code].....
and i need to create a .dat file and have it perform the tasks it states, what to do, I've just been looking for existing codes and trying to modify them because i have a very limited knowledge of programming.
I've created several methods in another class to perform certain matrix calculations. Do I need to have mutator method in this calculation class to define the column length of the matrix object? I understand why the following code is not working, but I was hoping to get a quick and easy workaround. I'm not sure how to initialize the size of the column for newMatrix within the method.
public int [][] sum(int matrixOne[][], int matrixTwo[][]) { int sumMatrix [][] = new int[matrixOne.length]["WHAT TO PUT HERE"]; for (int i = 0; i < matrixOne.length; i++) { for (int j = 0; j < matrixOne[i].length; j++) { sumMatrix[i][j] = matrixOne[i][j] + matrixTwo[i][j]; System.out.println("matrixSum" + sumMatrix[i][j]); } } return sumMatrix; }
Then I will use this method to print the new matrix:
public void printMatrix(int newMatrix [][]) { for (int i = 0; i < newMatrix.length; i++) { for (int j = 0; j < newMatrix[i].length; j++) { System.out.print(newMatrix[i][j] + " "); } System.out.println(""); } }
Is all of this overkill and should I just define all the methods within the same class to avoid these issues?
How do I create an instance of a class in a method?
I am a bit rusty whenever I think of instances. I always think of main method and objects when I see instance which gets me confused on what to do when I am not in a main method. The example:
I have a abstract class, School, and inside School I have some methods that must preform some action on an instance. For example, there is a move() method that must move the instance of School. Another method named, personOld(), which returns whether or not an instance of School surpassed some determined age.
How do I keep an item (TreeItem) from being selected when performing a drag -n- drop? I don't want it to be selected because it will end up making the target disappear (selection of TreeItem changes target panel).
Related question: Is there a way to "veto" a tree selection change as well?
The problem is, the values are completely different than they are saved in MATLAB, and probably I need to shift the values after transforming them into byte arrays.My Java code which reads values of floating numbers from a MATLAB bin file as follows:
import java.io.*; import java.io.File; import javax.imageio.ImageIO; import java.nio.ByteBuffer; // may be useful? public class floatingNumberMatrixReader {
I have in my application a button that when it is pressed, runs a long running task (usually takes a couple of seconds....).
My problem is that while the task runs, I want to stop the button from receiving clicks. Unfortunately this is something I have not achieved.
I have tried creating a new thread and calling the long running method, and using the Display.geCurrent().asyncExec...
Examples of what I have tried so far:
myButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { if (myList.size() > 0) { myButton.setEnabled(false); myButton.setGrayed(true);
[Code] .....
In both cases, while the button is disabled and the cursor busy, if the user clicks the button, the long running method is ran several times - and this is what I want to avoid.
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.
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");
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.
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;
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();