How To Avoid Getting Negative Numbers Of Coins

Oct 18, 2014

how to avoid getting negative numbers of coins, use casting and mod to show how many quarters, dimes, nickels, and pennies there are?

import java.util.Scanner;

public class VM
{
public static void main(String[] args)
{
//money deposit
Scanner input = new Scanner(System.in);

[code]....

View Replies


ADVERTISEMENT

Generate Two Arrays - One With All Positive Numbers And Another With Negative Numbers

Mar 10, 2015

Create an integer array with 10 numbers, initialize the array to make sure there are both positive and negative integers. Write a program to generate two arrays out of the original array, one array with all positive numbers and another one with all negative numbers. Print out the number of elements and the detailed elements in each array.

public class problem3 {
public static void main(String[]args){
int[] numbers = {1, 2, 3, 4, 5, -1, -2, -3, -4, -5};
for (int i = 0; i<numbers.length;){
if(i>0){
System.out.println(numbers);
}
else
System.out.println(numbers);
}
}
}

View Replies View Related

Negative Numbers In Binary

Mar 7, 2014

I need understanding why

1111 1101 = -3

View Replies View Related

Finding Binary Of Negative Numbers?

Mar 22, 2014

How do u find the binary of negative numbers? I already did it for positive numbers,?

View Replies View Related

Input Validation For Negative Numbers

Feb 20, 2014

I am very new to Java. I have been working for a couple months on a program for school. It has not gone well. I finally was able to scrap together a working program, but i left something out that needs to be. I have to include input validation to check for negative values, prompting users to re-enter values if negative. I have included my current code, the program works perfectly, but what to do about the negative numbers.

Java Code:

package gradplanner;
import java.util.Scanner;
public class GradPlanner {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numofclasses = 0;
int totalCUs = 0;

[Code] ....

View Replies View Related

Input Validation For Negative Numbers

Feb 20, 2014

I am very new to Java. I have been working on a program. It has not gone well. I finally was able to scrap together a working program, but i left something out that needs to be. I have to include input validation to check for negative values, prompting users to re-enter values if negative.I have included my current code, the program works perfectly, but what to do about the negative numbers.

package gradplanner;
import java.util.Scanner;
public class GradPlanner {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numofclasses = 0;

[Code] ....

View Replies View Related

Code Allows To Enter Negative Numbers -1

Apr 4, 2015

double CashInsert = Double.parseDouble(CashAmounttxt.getText());
System.out.println("Cash Received: " + "£" + CashInsert);
if (CashInsert<=0 &&(CashInsert <TotalPrice)){
ChangeLeft = CashInsert - TotalPrice;
System.out.println("Total: £" + TotalPrice);
System.out.println("Change Due: " + "£" + ChangeLeft);}
else {
System.out.println("Insuffient funds, please enter £"+ TotalPrice +" or More");}
}

#################################################

Cash Received: £-1.0
Total: £4.85
Change Due: £-5.85

It allows me to enter -1 I've already coded it so the person cannot enter less but -1 works.

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

Coin Toss (For Loop) - Reject Negative Numbers

Apr 3, 2014

I'm having some issues getting this code to reject negative numbers. What I'm doing wrong.

import java.util.Random;
import java.util.Scanner;
public class ForLoop
{
public static void main (String [] args) {
Random randomNumber = new Random();

[Code] ....

This is what I have so far..

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

Adding And Subtracting Positive Or Negative Two Numbers Of Any Length With Strings?

Feb 3, 2014

You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.

The program must use a "Number" class which includes at least the following methods:

Number ( );
Number (double n);
Number add (Number RHS);
Number subtract (Number RHS);
String toString ( );

This is what i have but it only adds positive numbers and it doesn't subtract problems like 7.05-8.96. Also some of it was what our teacher gave us like alignwhole method

import java.util.Scanner; 
public class Number{
private String whole;
private String decimal;
private String sign;
  public static void main (String[] args){
 System.out.println("Enter two numbers");

[code]....

View Replies View Related

Adding And Subtracting Positive Or Negative Two Numbers Of Any Length With Strings

Feb 5, 2014

You are to design a Java application to carry out additions and subtractions for numbers of any length. A number is represented as an object which includes a sign and two strings for the whole and decimal parts of the number. And, the operations must be done by adding or subtracting characters directly. You are not allowed to convert these strings to numbers before the operation.

The program must use a "Number" class which includes at least the following methods:

Number ( );
Number (double n);
Number add (Number RHS);
Number subtract (Number RHS);
String toString ( );

The below code is what our teacher gave us to start with, but it needs to add and subtract positive or negative numbers of any length. This code only adds positive numbers. Need to write code for subtraction .

Java Code:

import java.util.Scanner;
public class Number{
private String whole;
private String decimal;
private String sign;

[Code] .....

View Replies View Related

Averaging Grades Program Throwing Exception With Negative Numbers

Nov 17, 2014

So I have re-written the code but it is still not running correctly. Any number i type in it throws an exception, also i need the program to add the totals that i type in and then once i type -1 into the prompt button list all the number i typed in and give me the average.

import java.awt.*;
import java.awt.event.*;
import javax.swing .*;
import javax.swing.text.*;
public class Averages extends JFrame
{
//construct components
JLabel sortPrompt = new JLabel("Sort By:");

[Code] .....

View Replies View Related

How To Create A Coins Program

Feb 16, 2015

Ive been trying to create a program to say how many 5cent,10 cent and 50 cent coins there are if you put a number in example 20. I used the init method because thats what the teacher told me to use but i have no clue on what to do.

View Replies View Related

Java Programming - Coins In A Jar

Apr 3, 2014

We are working with Java and Eclipse..One of the assignments asks to "Write an application that determines the number of coins in a jar and prints out the total in dollars and cents. Read integer values that represents the number of quarters, dimes, nickels and pennies. Use a currency formatter to print the output."

View Replies View Related

Tossing Coins For A Dollar Java Program?

Jan 29, 2014

The question is we have to write a class named Coin. The Coin class should have the following field:

A string named sideUp. The sideUp field will hold either "heads" or "tails" indicating the side of the coin that is facing up.

The Coin class should have the following methods:

A no-arg constructor that randomly determines the side of the coin that is facing up ("heads" or "tails") and initializes the sideUp field accordingly.

A void method named toss that simulates the tossing of the coin. When the toss method is called, it randomly determines the side of the coin that is facing up ("heads" or "tails") and sets the sideUp field accordingly.

A method named getSideUp that returns the value of the sideUp field.

Now we have to create a game program using the Coin class. The program should have three instances of the Coin class: one representing a quarter; one representing a dime, and one representing a nickel.

When the program begins, your starting balance is $0. During each round of the game, the program will toss the simulated coins. when a coin is tossed, the value of the coin is added to your balance if it lands heads-up. For example, if the quarter lands heads-up, 25 cents is added to your balance. Nothing is added to your balance for coins that land tails-up. The game is over when you balance reaches on dollar or more. If your balance is exactly one dollar, you win the game. You lose if your balance exceeds one dollar.

I think I've completed the Coin class but the game program is giving me problems. I got it to add the balances when they land on heads but it only does it one time. I'm trying to figure out how to input a loop but I haven't had any luck.

CoinClass1.JPGCoinClass2.JPGGameProgram1.JPGGameProgram2.JPGGameProgram3.JPG

View Replies View Related

Code That Prompts For And Reads In The Number Of Coins In One Of The Piles

Oct 26, 2014

I'm very new to writing code (college freshman) and I need a code that prompts for and reads in the number of coins in one of the piles, as well as a String containing a single letter to indicate the type of coin in that pile: "P" for pennies, "N" for nickels, "D" for dimes, and "Q" for quarters. The program then computes the value of that pile of coins and prints it out. Here's what I have so far:

import java.util.Scanner;
/**
This program reads a student's final class average,
and prints out the appropriate letter grade..
*/
public class Coins
{
public static void main(String[] args)
{
// Define constants

[Code]...

View Replies View Related

Vending Machine - Tell User Combination Of Coins That Equals To Amount Of Change

Jan 25, 2012

I have a simple java program to write which tells the user combination of coins that equals to the amount of change i.e:

user input 87

output:

3 quarters
1 dime
0 nickels
2 pennies

How the program remembers the remainder which is passed to the next column of let say dime

i.e

originalAmount = amount;
quarters = amount /25;
amount = amount % 25; <---- this is confusing for me?!?! how can the integer = integer % 25
dimes = amount / 10; <--- HOW THE PROGRAM remembers the "remainder" instead of the original user input as the code it self tells you dimes = amount where "amount" is what user input NOT remainder.

amount = amount % 10;
so on ....;

What I don't understand is HOW this algorithm works. I mean we have int amount where user inputs the number we get the first calculation amount/25 = how many quarters and then amount %25 WILL tell us about the reminder. By looking at this piece of code I would say that the system should start the calculation for the dimes again from the original number since the code says dimes = amount/10 AND amount = amount%10. My understanding is that the calculation should be done from the original user input.

Book or code it self is not clear for me how the reminder is "REMEMBERED" and then pass on to the next calculation>!?!?

UNLESS the code: amount=amount%25 gets the remainder so the next code under it is REQUIRED to read from the last prompt code.

View Replies View Related

How To Avoid NullPointerException

May 3, 2014

I have been playing around with my code, but how to avoid NullPointerexception.. So my program's point is simple, use Jsoup to scrape html of certain webpage, then i search the things i want and print them out. Problem is, when scraped html doesnt contain even 1 thing on my search list, i get NullPointerException... i understand why, Heres part of my code:

Java Code:

//Things i need to search from html
String[] MySearchArray = new String[]{"138","146","474"};
//Search things contained in MySearchArray and print them out
for (String Ml : MySearchArray) {
Element flights = doc.select(String.format("tr:contains(%s)", Ml)).first();
Elements flights2 = flights.select("td");
System.out.println(flights2.get(4).text() + " " + flights2.get(0).text()+ " " + flights2.get(3).text());
} mh_sh_highlight_all('java');

View Replies View Related

How To Avoid Empty Map Value In A List

Apr 24, 2014

I have a HashMap with multiple values at runtime. In that map one key has empty value , how to avoid this value to add in a list. some code sample is below:

public HashMap getLoop2Map(Map map, String xslFile , int sheetNo){
HashMap hashMap = new HashMap();
try{
ArrayList list = new ArrayList();
System.out.println("-------Map : " +map);
list.add(map.values());
//System.out.println("------- boolean : " +val);
System.out.println("------List : " +list);
}

I do not want to add the empty value in a list ...

result in map

-------Map : {Free Text Entry={},
Mouth / Throat={Free Text Entry={Free Text Entry=<FORMFILENAME>EditChartPhysicalExamText.form
</FORMFILENAME><TAG>"<MUSCULOSKELETAL.PE>"</TAG>},
Salivary Glands Condition={Salivary Glands Condition=MouthAttribute},
Examination Overview- Mouth / Throat={Examination Overview- Mouth / Throat=MouthAttribute},
Tonsils={Tonsils=MouthAttribute},

[Code] ....

View Replies View Related

Avoid Duplicates On ArrayList

Nov 5, 2014

I'm struggling with that piece of code, my intention is to check for the object I want to add before adding it, so there won't be any duplicate on my list. I'm not sure how could I do that, since I'm working with objects.

Person is a class with few parameters such as id, name, and few others.

I guess I should search for a person with the same id, since that has be unique, but can't get it right.

private ArrayList<person> model= new ArrayList<>();
//...
if (model.contains(person))throw new IllegalArgumentException("duplicate");
else model.addElement(person);

View Replies View Related

Avoid Deadlock While Coding

Jul 25, 2014

is it possible to avoid deadlock while coding..

View Replies View Related

How To Avoid Hardcoded Href URL In JSP

Feb 12, 2014

How to avoid hard coded href url .

For example a href="www.yahoo.com">

View Replies View Related

How To Avoid Keyboard Auto-repeat

Oct 8, 2014

I wanted to make a small program to move a small rectangle by pressing the WASD keys. The program works, except that when I hold a key to move the rectangle, after a second, auto-repeat starts up, and the rectangle motion accelerates. I want to prevent automatic repeat to activate, so that the rectangle moves at a constant speed when I hold a key and stops when I released. Here is the ButtonMotion classe :

import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.KeyStroke;

[code]....

View Replies View Related

How To Avoid Hard Coded Values

Jun 17, 2014

I have code which query value from database,the use case is the user can enter value as 1,01,11 in database but when the user enter value in xml file he can only enter 11,01,12 in database there is two columns lets say column test1=1 and test2=2 combination of this is 12,which is the value the use will enter in xml, but sometime the use can enter test1=04 than column test2=00 ....

View Replies View Related

EJB / EE :: Avoid Persist Entity On Set Methods

Dec 9, 2014

I'm using EclipseLink, WildFly, EJB, postgresSQL and JSF.I'm trying to persist some countries and their localities.So I've:

- Two entities Countries and Localities in which I specify respective columns and relations.
- Abstract Session beans for entity classes: AbstractFacade providing basic crud methods and entity manager.
- Two concrete session bean for entity classes: CountriesFacade and LocalitiesFacade.
- A JSF managed bean named geoJSF.
- A JSF page with a form allowing to insert new country and localities.

In geoJSF I'm injecting via EJB CountriesFacade as property named cf and LocalitiesFacade as property named lf.For the actual inserting country and locality I'm using geoJSF.country and geoJSF.locality. When the form is submitted I'm simply doing:

this.cf.create(this.country);
this.locality.setCountry(this.country); //<- this throw an exception (unique constraint violation) due to the attempt to reinsert this.country
this.lf.create(this.locality);

I disabled all cascade among relations definitions.Based on what I know this.country should appear detached to entity manager so, setting relation the entity manager try to re-persist it.

View Replies View Related







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