Symmetric Test Returning Negative For Some Reason
Jan 25, 2015
So I had to make a 2 symmetric algorithms. I made 1 that accepts an int as an argument and returns the binary String representation and another that accepts the string of 1's and 0's and returns the decimal int representation.
By all means these should be inverse symmetric algorithms. I even call both in one method call and sure enough I get the same argument returned as a result. But when I try and do the boolean symmetry test, it comes back false.
import java.util.Scanner;
public class InverseFunctions
{
public static void main(String[] args {
int x;
String s;
[Code] ....
View Replies
ADVERTISEMENT
Apr 19, 2015
The question is write to a method symmetric that accepts a stack of integers as a parameter and replaces the stack contents with itself plus a symmetrical version of itself (the same elements in the opposite order).
For example, suppose a variable s stores the following elements:
bottom [10, 50, 19, 54, 30, 67] top
After a call of symmetric(s),the stack would store the following elements
bottom [10, 50, 19, 54, 30, 67, 67, 30, 54, 19, 50, 10] top
Note that the symmetric version is added on to the top of what was originally in the stack. The bottom half of the stack contains the original numbers in the same order.
If your method is passed an empty stack, the result should be an empty stack.
If your method is passed a null stack, your method should throw an IllegalArgumentException.
a) Write the method symmetric using one temporary stack and one temporary queue.
/> Re-write the method using only one temporary Queue.
What I have done so far is
public static Stack symmetric(Stack s1){
Stack s2 =new Stack();
int theTop=0;
if(s1.isEmpty()){
return s1;
[Code] .....
Its not working.
View Replies
View Related
Mar 27, 2014
I am using Netbeans. For some reason I cant display an image.
Java Code:
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
public class ImageLoadingTutorial extends Applet{
private Image spiral = null;
[Code] ....
View Replies
View Related
Mar 3, 2012
interface Interface{
class B{
}
}
I know that we can create an inner class inside an interface but i want to know that why we'll create an inner class inside an interface. I mean what is the use of creating inner class inside an interface and what is the advantage of it.
View Replies
View Related
Nov 11, 2014
when i input a positive integer it works but when i input a negative number it doesn't work
my pseudo code:
READ input
WHILE( NOT CORRECT INPUT)
READ INPUT AGAIN;
ENDWHILE
DECLARE array arr[input]
FOR(i=0 to input-1)
arr[i]= Random number from 0 to 100;
ENDFOR
DISPLAY ARRAY
error message when i input -5 : Exception in thread "main" java.lang.NegativeArraySizeException atPosNeg.main<PosNeg.java:36>
import java.util.*;
class PosNeg{
public static void main(String args[]) {
Random generator = new Random();
Scanner scan = new Scanner(System.in);
[code]....
View Replies
View Related
Mar 2, 2015
How do you replace negative zero value with a zero value when Printing results:
I want to remove the minus sign in -0.0000 and instead have 0.0000.
I am Printing coordinates and I do not want to have negative zero.
the coordinates are defined as double.
System.out.format(java.util.Locale.US," %.4f %.4f %.4f %.4f%n"xCur ,yCur,xNext,yNext);
0.0000 1.0000 -0.0000 0.0000
View Replies
View Related
Mar 7, 2014
I need understanding why
1111 1101 = -3
View Replies
View Related
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
Jun 3, 2014
char c=(char)-65;
This is legal but how ?? what is the value actually being stored in c ? The output is shown as .
View Replies
View Related
Mar 22, 2014
How do u find the binary of negative numbers? I already did it for positive numbers,?
View Replies
View Related
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
View Related
Mar 30, 2015
I am new to Android. I have byte array of size 10. I am passing the Decimal values (131 - 140) to byte array. But while printing I get Negative (-) values with decreasing order .
How can I get same value as positive values?
Or How can I store positive value e.g. 131 as byte array element.
Please not my requirement is array must be ByteArray only
View Replies
View Related
Sep 18, 2014
I am not sure what is happening with my code, but it is giving me a negative number. I am trying to write a program that calculates the product of the odd integers between 1 and 25. I messed with the program and as soon as you enter a number over 22, the end result is a negative number.
int total = 1;
for (int i = 1; i <= 25; i += 2){
total *= i;
}
System.out.println("Product:" + total);
View Replies
View Related
Jan 21, 2014
I am trying to raise a real number to a negative value e.g x-y(x raised to power -y) which can also be written like 1/xk . I am trying to run th program each time bt it doesnt give me the desired result.
Here is the code
public class RaiseRealPower {
public void run(){
double value =readInt("Enter value ");
double power =readInt("Enter power ");
System.out.println("Answer is " +raiseIntToPower(value,power));
[Code] ....
View Replies
View Related
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
Jul 20, 2014
Ask the user to enter a sequence of at most 20 nonnegative integers. Your program should have a loop that reads the integers into an array and stops when a negative is entered (the negative number should not be stored). Invoke the average method to find the average of the integers in the array (send the array as the parameter).
how can I remove the negative number from the array and calculate the average of the posive elements without the negative ones? This is my code so far...
import java.util.Scanner;
import javax.swing.JApplet;
public class Parameters
{
//-------------------------------------
//Calls the average and minimum methods
//with different numbers of parameters
[code]....
View Replies
View Related
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
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
Apr 2, 2014
How would I go about inputting the negative values in the array in case 1 the array comes from the user, case 2 from the text file? The sum prints perfectly fine with positive values but if I input negative values it just completely ignores them.
case 1:
int sum;
System.out.print("Enter list of comma-delimeted integers: ");
Scanner scan = new Scanner(System.in);
String input2=scan.next();
String[] num = input2.split(",");
int[] a= new int[num.length];
[Code] ....
View Replies
View Related
Feb 4, 2015
The program work somehow, but it can't count the first input when user key in.
import java.util.Scanner;
public class DetermineValues {
public static void main( String[] args ) {
int sum;
int minus;
int data;
[code]....
View Replies
View Related
Oct 3, 2014
I am attempting to write a program that reads a nonnegative integer and computes and prints its factorial. So far I have: Java Code: import java.util.Scanner;
public class Chapter3point37 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int nonNegative = 5;
int count=1;
int product=1;
int factor=1;
System.out.println("Input a nonnegative integer: ");
nonNegative = input.nextInt();
[code]...
how I should correctly prompt the user to input the values.
View Replies
View Related
Feb 7, 2015
I have a problem where I am trying to re arrange the values in an array from negative to positive. I have it re arranged but I cannot figure out how to re arrange them in numerical order. I have to use O(n) and O(1) operations.
Java
import java.util.Arrays;
public class Task7 {
public static void main(String[] args){
int[] numbers = {-19, 6, 34, -3, -8, 23, 5, 678, -45, -12, 76}; //array of positive and negative numbers
int next = 0; //in no particular order
[Code] .....
View Replies
View Related
Apr 4, 2014
i am writing a program that would be used for ticket sales and their are 100 tickets and a person can only buy 4 at a time. i need my program to not go negative at the end and also i need to stop the user from entering a negative number.
import java.util.Scanner;
public class ticketmaster {
public static void main(String[] args) {
// Variable decoration uses strings , doubles and final doubles.
Scanner in = new Scanner(System.in);
[code]....
View Replies
View Related
Apr 4, 2014
i am writing a program that would be used for ticket sales and their are 100 tickets and a person can only buy 4 at a time. i need my program to not go negative at the end and also i need to stop the user from entering a negative number here is the code.
import java.util.Scanner;
public class ticketmaster {
public static void main(String[] args) {
// Variable decorationuses strings , doubles and final doubles.
Scanner in = new Scanner(System.in);
[code]....
View Replies
View Related
Dec 10, 2014
I'm taking a class in object oriented programming and we have a task to write a method that returns positive, negative or zero depending on the sum of two variables.
I've had a go at it and i've got to a certain point but i'm struggling to get past this on error - return outside method.
public class Test {
public int sumArgs;
public int arg1;
public int arg2;
[Code] ....
The error occurs on the line 'return "Positive";
View Replies
View Related
Feb 3, 2015
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
/**
An applet that shows a rotating globe.
[Code] ....
View Replies
View Related