Handling Fraction As Calculator In Specific Form?

Nov 6, 2014

I am supposed to make a code which can handle fractions as a calculator in the form of 1_1/2 + 1 = 2_1/2

This is what I've done so far and now im just lost.

Heres my code:

import java.util.Scanner;
import java.util.StringTokenizer;
public class FracCalc {
public static void main(String[] args ){
// read in the first expression

[Code] ....

View Replies


ADVERTISEMENT

Fraction Calculator Prints Correct Answer But Not In Decimal

Feb 23, 2015

package FracCalc;
import java.util.Scanner;
public class FracCalc {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
 
int n1 = 0; //n1 = first numerator user have to enter
int d1 = 0; //d1 = first denominator user have to enter
int n2 = 0; //n2 = second numerator user have to enter
int d2 = 0; //d2 = second denominator user have to enter
int numeratorAnswer = 0; //calculates numerator of an equation
int denominatorAnswer = 0; //calculates denominator of an equation
 
[code]...

I mean its not that important but i figure it will be better if I could print out the answer both in fraction format and decimal format. while my answer prints in fraction just fine how do i show it in decimal?

System.out.println("");
 
System.out.print("And ");
System.out.print("answer in Decimal is: ");
 
decimalAnswer = ((double)numeratorAnswer/denominatorAnswer);
System.out.println((float)decimalAnswer);

View Replies View Related

File Handling In Java - Modifying Specific Line

Mar 12, 2015

How to modify a specific line of a file in java. File name must be accepted from command line.

View Replies View Related

Is Handling Instance Of Error Or Its Subclass Is Also Called Exception Handling

Mar 7, 2014

I have studied about the hierarchy of exception classes in which Throwable class comes at the top and two of its direct subclasses are Error and Exception..I just want to ask if in some code snippet we throw an instance of Error or its subclass within the try catch block then will that be also called "exception handling" ? I am asking this because Error class is not a child class of Exception therefore cant be said an Exception, therefore handling the same should not be called exception handling

View Replies View Related

Write A Specific Byte Sequence To A Specific Memory Location On A Removable Storage Drive?

Jan 29, 2014

I am trying to write a specific byte sequence to a specific memory location on a removable storage drive. Does Java allow me a way to do this? I know the dangers in accessing memory, but the memory location of the data that will be written will never change.

how to assign a variable a memory location.

View Replies View Related

Swing/AWT/SWT :: Move Focus From Specific Jcombobox To Specific Jtextarea?

Nov 5, 2014

How do I move focus from a jcombobox to a specific component say a jtextarea.

My attempt below seems to be moving to a random component not the desired jtextarea.

takenByCombo.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_TAB ) {
e.consume();
dayJTArea.requestFocus(); // focust not moving dayJTArea
}
}
});

I applied the above logic to move focus from a specific jtextarea to another jtextarea as seen below and it works as desired.

dayJTArea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_TAB ) {
e.consume();
monthJTArea.requestFocus();
}
}
});

View Replies View Related

Changing Value In Specific Point Of Specific Line In TXT File

Feb 9, 2015

So here we go with my problem:

- from the main class will arrive three variable (String name_used, int level_choose, int level_result)

I have a .txt file with this kind of formatting:

mario 1 1 0 1 0 1
carlo 0 0 0 1 1 0
...

Where I use 1 and 0 in the main for write if the level (you see that the numbers are always sixr? are egual to six level existing) BEFORE is done correct or wrong

- when in the main a user make a level a feedback coming back from the class level saying if the user made the count correctly or wrong. and i wanna replace the value (1 or 0) in the txt file with the new level result.

So i have all what i need as parameters i think.

name_used to look for the correct line in .txt file with .indexOf
level_choosed to go throught the correct index of that line
level_result (1 or 2) to be replaced with the existing one

Java Code:

public void salvaRisultati(String name_used, int level_choosed, int result_of_level) throws FileNotFoundException{
}
} mh_sh_highlight_all('java');

View Replies View Related

Servlets :: HTML Form - Possible To Send Data (image / Text) Along A Single Form

Jan 8, 2015

I want to understand how is it possible to send data (image + text) along a single form. Here is my code:

<form method="post" action="updateAccount"
encType="multipart/form-data">
<input type="file" name="file" value="Select image ..." /> <input
type="submit" value="Start Upload" /> <br>
<textarea rows="8" cols="54" name="about">Yes

View Replies View Related

Subtracting A Negative Fraction?

Mar 10, 2014

For some reason, I'm getting the correct result, but my negative sign is having issues. For example, if I do 1/4 - (-2/4), I get (-3/4).

Here is my minus method for subtracting fractions.

/**
Subtracts a fraction from another fraction.
@param toUse, the fraction to subtract.
@return minusFraction, the result after subtraction.
*/
public Fraction minus(Fraction toUse)

[Code] .....

Here is my reduce() method, just in case...

/**
Reduces the fraction, if possible, to it's simplest form.
Converts negative fractions to the form -x/y, or if -x/-y --> x/y
*/
private void reduce() {
int lowest = Math.abs(numerator);
int highest = Math.abs(denominator);

[code]...

I only switched an operator from my previous addition method, given here as well. I think only switching the + to a - may have caused my issue.

/**
Adds two fractions together.
@param toUse, the fraction to be added.
@return plusFraction, the sum of the two fractions.
*/
public Fraction plus(Fraction toUse) {

[Code] .....

View Replies View Related

Changing Double To Fraction

Jan 6, 2015

I was wondering how you would change a double to a fraction.

For Example:
double x = .5

From this we know that the fraction is obviously 1/2 (simple form of course), but what are the steps necessary to convert the double to the fraction 1/2?

View Replies View Related

Making Payroll Form Using Java Language But Its Like Framing Form?

Jun 6, 2014

import java.util.Scanner;
public class Exercise1{
public static void main(String[] args) {
String employeeName, employeeNumber, position, department ;
double otpay, salary, deduction, hrs, rate ;
Scanner input = new Scanner (System.in);

[Code] ....

That's my codes but its wrong according to our prof. it should be in frame form. i don't know how to do it since i did not encountered framing since i was started in java.

View Replies View Related

Check Specific Value From A Specific Line From TXT File

Feb 8, 2015

I have some problem to understand the way to make this:

In my main class a user can save his name in a txt file (and the system initially will add 6 value equals to 0) than he can choose between 6 level and make it.

example of .txt file data:

mario 0 0 0 0 0 0
carl 0 0 0 0 0 0

AT THIS MOMENT i just made other class and they work, is this new one that is hard for me. I'm trying to make a class that:

1- (first method called verificaRisultati) take name_used and level_choosed from the main and go to check in the .txt file if that level before was done right(1) or wrong(0)

and return something like "before you made this level properly" or "before you made this level incorrectly" AND THEN let the user start with the level.

2- (second method called salvaRisultati) at the end of the level i wanna pass the result (correct/incorrect) to another method of this class that will save the value (1 or 0) associated to the user in the right position.

This is the class that i'm writing:

Java Code:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class ResultUsers {

[Code] ....

I really need some hint and some code example because I'm stuck. How I can take exactly the line with the user name? How I can correctly split the line in an array and then read/modify the value for that level?

View Replies View Related

EJB / EE :: Maintaining Values From Primefaces Form After Form Submit

Jan 29, 2015

I have a form containing several fields, 2 of which persist to different table in a database than the rest of the fields on the form. I have no problem persisting the data into both tables of the database, and after the form is submitted I reset the form to its default values. That all works fine.

But in the same session, when I open another form (a search form) and enter search criteria, which then displays a datatable containing the search results, those 2 values that are persisted to another table are not showing up, but the rest of the data is.

Here is the method that calls the persist methods:

@ManagedBean(name = "foreignPartyController")
@SessionScoped
public class ForeignPartyController implements Serializable {
...
public void saveData() {

[Code].....

The values do show up, but the problem is, when a subsequent form is opened in the same session (e.g. a search form) the field for that value shows the actual value, instead of the field being blank.'

I am not sure why the data from the one database ("parent") is showing up, yet the data from the other database ("child") is not.

Is it something I am doing wrong? I thought by setting the setter in the child controller class back to a new instance of the Entity class (PolicyPayment) that it would reset the form to default values, but at the same time retain (or save) the inputted values in the same session.

View Replies View Related

Creating Methods Using Arrays - Fraction Class

Sep 27, 2014

I am trying to get the average of 3 different fraction arrays. I made a fraction class and I made methods such as read() and average() in this new class.

package fractions;
import java.util.Scanner;
import java.util.Arrays;
public class FractionArrays {
public static void main(String[] args) {
Fraction completeFraction = new Fraction(5,6);

[Code] ....

I was wondering if there was any way to use the arrays I created in the read method in the average method. If I find that out I should be able to do it on my own. Is there a way to make the arrays public to use in different methods?

View Replies View Related

JSP :: Synchronous Form To Asynchronous Form Submit?

May 8, 2014

I have a button on UI which adds messages and when the user clicks on it the form gets submitted, meanwhile the user is clicking on refresh(F5) multiple times which is causing the same message to be displayed multiple times. To resolve this , I am converting the form from a synchronous submit to Asychronous but it is still not working. Below is the code:

Code before:

<td><input class="buttonred" type="submit" value="Confirm Add" name="submit_message"></td>
<s:form action="upd-message" method="POST" validate="true" onsubmit="validateMsg();return false;" enctype="multipart/form-data">
function validateMsg() {
var frm = document.forms["upd-message"];
frm.actionType.value=message;

[Code] .....

View Replies View Related

Java Fraction Calculation Compiles But Output Is Wrong

May 9, 2014

So all the errors in the code is fixed but its now got the wrong output

what I get:

Enter numerator; then denominator.
5
8
5/8
Enter numerator; then denominator.
4
10
4/10
Sum:0/0

it should be:

Enter numerator; then denominator.
5
8
5/8
Enter numerator; then denominator.
4
10
4/10
Sum: 82/80 1.025
Product: 20/80 0.25
Enter numerator; then denominator.
6 0
infinity

here is the code

Fraction.java
public class Fraction { //declares that it's a class that can be intantiated
private int numer; // private intager for numerator
private int denom; // private intager for denominator

[Code] ....

View Replies View Related

Fraction Class - Calculating Less Than Percentage GCD On Negative Numbers

Feb 15, 2014

I've been writing a fraction class code below that does a number of arithmetic calcs and when I run it these are the results I get. My gcd doesn't work when it comes to negative fractions and I'm not quite sure how to print.out the boolean methods ((greaterthan)), ((equals))and ((negative)). I'm also not sure if I have implemented those 3 methods properly. I'm still learning how to do unit testing.

Enter numerator; then denominator.
-5
10
-5/10
Enter numerator; then denominator.
3
9
1/3
Sum:
-5/30
-0.16666666666666666
Product:
-5/30
-0.16666666666666666
Devide:
-15/30
-0.5
subtract:
-45/90
-0.5
negative:
1/6
0.16666666666666666
Lessthan:
1/6
0.16666666666666666
greaterthan:
1/6
0.16666666666666666

FRACTION CLASS

import java.util.Scanner;
public class Fraction
{
private int numerator; //numerator
private int denominator; //denominator

[Code] ....

View Replies View Related

Relate Fraction Objects With Read / Write Methods Used In Binary I/O

Apr 25, 2014

We're learning how to use Binary I/O commands...which equates to....

My issue is trying to relate the Fraction objects (which we are to create using a loop) with the read/write methods used in Binary I/O (input/output streams). I left a blank after the output.write(), so you can see where the issue exist.

Java Code:

import java.io.*;
public class FractionTest {
public static void main(String[] args) {
int [] fraction = new int[3];
for(int i = 0; i <= fraction.length; i++){
Fraction numbers = new Fraction();

[Code] ....

View Replies View Related

JSF :: How To Post PHP Form Contents To Form

Feb 28, 2014

How do i post php form contents to a jsf form?

View Replies View Related

File Handling In A GUI

Apr 28, 2014

I created a GUI using Java with Buttons, Pictures and labels.When one of the buttons is pressed it increases the price, which can be seen by the labels increasing the necessary amount.What I would like to be able to do in my GUI is allow the user to record details of the sale and be able to display the sales of past customers to date, by this I mean when I click one of the buttons it will record the amount of that item into a file and when I press the another button, not the same button as the items, it will show the user the existing orders that have been taken in the past.

Here is my Code:

//Importing needed classes
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
 
[code]....

View Replies View Related

Handling IO Synchronization

Sep 16, 2014

I have a class (WriteExcel) which writes to an excel file. For those who don't know, Excel doesn't handle multiple access well (or at all).

I then have another class (WriteManager) which creates a new thread for each write command, and then calls a method from the WriteExcel class.

Other classes in my project calls methods from WriteManager to send their requests. My situation looks like this:

1) Thread 1: Someclass -> WriteManager.write1stPage(write) -> Creates Thread 2 -> WriteExcel.write1stPage(write) -> Start Excel Write
2) Thread 1: Someclass -> WriteManager.write2ndPage(write) -> Creates Thread 3 -> WriteExcel.write2ndPage(write) -> Start Excel Write
3) Thread 1: Someclass -> WriteManager.write3rdPage(write) -> Creates Thread 4 -> WriteExcel.write3rdPage(write) -> Start Excel Write
4) Thread 2: Finished writing and ready to save -> Throws exception due to multiple Excel access
5) Thread 3: Finished writing and ready to save -> Throws exception due to multiple Excel access
6) Thread 4: Finished writing and ready to save -> Throws exception due to multiple Excel access

I need to figure out a way to restrict access to WriteExcel until the writing is finished. So I need something like this:

1) Thread 1: Someclass -> WriteManager.write1stPage(write) -> Creates Thread 2 -> WriteExcel.write1stPage(write) -> Start Excel Write
2) Thread 1: Someclass -> WriteManager.write2ndPage(write) -> Creates Thread 3 -> Waits Until WriteExcel is free
3) Thread 1: Someclass -> WriteManager.write3rdPage(write) -> Creates Thread 4 -> Waits Until WriteExcel is free
4) Thread 2: Finished writing and ready to save -> Saves and frees WriteExcel
5) Thread 3: WriteExcel is free -> WriteExcel.write2ndPage(write) -> Start Excel Write
6) Thread 3: Finished writing and ready to save -> Saves and frees WriteExcel
7) Thread 4: WriteExcel is free -> WriteExcel.write3rdPage(write) -> Start Excel Write
8) Thread 4: Finished writing and ready to save -> Saves and frees WriteExcel

What is currently the best approach in java for handling this sort of situation?

View Replies View Related

Packages And Exception Handling?

Aug 17, 2014

Is there any connection between packages and exception handling in java. Means is it necessary to create a package before trying exception handling examples?

View Replies View Related

Exception Handling In A Constructer

Apr 22, 2014

Is there a special mechanism through which exception can be handled in a constructor?

Suppose while creation of an object there occurred an exception while creating an object, and the object is half constructed. How do we make sure we handle this kind of exceptions in a constructor?

View Replies View Related

Handling Sprite Overlapping?

Sep 16, 2014

The game I am working on now is an overhead game. Anyways I have a visual issues with the sprites where they overlap. I mean its normal to have sprites overlap but I do not want one to appear like it is walking on top of the other rather than the ground. The below is an image illustration the issue I want to address. (I am really good at drawing)

On the right you see what I want my sprites to do, but on left you see overlap in the way I am trying to avoid.

Now my approach to this problem is taking my array of monsters (a class that extends JLabels) and reordering them based on which monster is higher I make this occur at every run of my thread. It seems to me that if a monster is higher it should be added to the screen before the one that is lower. So I try to keep resorting the array of monsters at every iteration of my main game loop based on who is higher to account for the overlap

if (e.getSource() == follower){
c =0;
for (monster m: monsters) {
//Arrays.sort(monsters, new whoHigherComparator()); //I tried having the sort happen here
m.movingToPlayer(joe);//1
Arrays.sort(monsters, new whoHigherComparator());//2
layeredScene1.add(m, new Integer(3));//3

[code]...

The above is code that shows the majority of my games flow I will describe my flow with references to the commented lines above with [num] for better understanding of where I am coming from. Basically, there is a set number of monsters, the monster all locate and move to the player [1], then I attempt to sort the monster array based on height [2], then add my re-sorted monsters back onto the pane in order [3], then bumpers (a type that handle collision and make sure monsters don't overlap too much) attach the current monster [4], if bumpers are touching monsters arae moved away from each other until their bumpers are no longer touching [5], if the monster is moving leftwards set the animation to a leftwards walk [6], and likewise for rightwards walking [7]

By the way this is my comparator

public class whoHigherComparator implements Comparator<monster>
{
@Override
public int compare(monster m1, monster m2) {
System.out.println("comping?");
int h1 = m1.getY();
int h2 = m2.getY();
if (h1 < h2)

[code]...

At this point everything in my game flow works just fine, its just the annoying "walking on top of each other" effect that I am having trouble handling. And my attempt at this is line [2] & [3] from my game flow which seems like it would be an effective way to handle the "walking on top of another" effect but its surely not working. I also swapped the returns from my comparator but still no changes. It seems that maybe the array isn't being sorted and/or these changes are just not being reflected.

View Replies View Related

Handling Large Numbers

Oct 31, 2014

I found an exercise online to create a small program . I have this code that I have done so far:

import java.util.Scanner;
public class Test {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
long a = sc.nextLong(); long b = sc.nextLong();
long count = 0; // counter

[code]....

The program should read the numbers a and b and list how many numbers between a and b are divisible by either 2, 3 or 5. If the user types the number 5 and then 8.... it would look at all the numbers in between - 5,6,7,8 and check if any of them are divasable by either 2,3 or 5. And since the numbers 5,6,8 are it would return the number 3 to the user..Now the problem is that this program only works for small numbers, but when I try to input numbers such as 123456789012345678 and 876543210987654321... it doesn't run at all.

So there needs to be a quicker way on how the program checks the numbers divisibility instead of checking each one. Here is where I am lost. How to fix the program that it will read bigger numbers such as a=123456789012345678 b=87654321098765432..There must be a quicker way ...something that can modify the code so it finishes in the matter of seconds not hours. Something that will fasten the process of checking if the numbers are dividable.

View Replies View Related

Exception Handling For Strings?

Nov 25, 2014

For one of my last labs for the semester, my professor is having the class go back to our very first program and apply some of the exception handling that we just recently learned about. Here's my improved code so far:

Java Code: import java.util.*;
import java.lang.*;
public class Lab2Part1 {
public static void main (String [] args) {
Scanner input = new Scanner (System.in);

[code]....

My code compiles fine, but even if I enter an integer or a double, it saves the number as a string, and prints that out as the name. Is there any way to get around this? Or do I need to use something besides a try-catch?

View Replies View Related







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