Shuffling Array Returns All Zeroes?

Sep 11, 2014

I have to shuffle a deck of cards (supposed to be 52 but I started with 3) and I wrote this code but it returns all zeroes for the RandomDeck.

import java.util.Random;
public class shuffleDeck {
public static void main(String[] args) {
int[] Deck = new int[3];
for (int i=0; i<3; i++)

[code]...

View Replies


ADVERTISEMENT

Shuffling Array - Swapping Every Element

Aug 25, 2014

While shuffling an array, if I use Collections.shuffle(), there is a chance that an element in a particular index in the input array can be present in the same index in the output array. Is there an existing method that handles that too? If not, how can I best handle it? After shuffling, will swapping every element with the last element work?

View Replies View Related

Shuffling Array Is Putting Same Integer In More Than One Place

Sep 11, 2014

I have to shuffle a deck (array) of 52 integers but I started with 3 for testing if it was an even shuffle and it will place the same integer in more than one spot in the random array. I'm not sure what I'm doing wrong...

import java.util.Random;
public class shuffleDeck {
public static void main(String[] args) {
int[] Deck = new int[3];
for (int i=0; i<3; i++) {

[Code] ....

View Replies View Related

Method That Returns New Array By Eliminating Duplicate Values In Array

Jun 15, 2014

Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: public static int[] eliminateDuplicates(int[] list). The thing is that I found the working solution that is written below, but how it works. How to eliminateDuplicates method done with flag and flag2.

Here is the code:

Java Code:

import java.util.Scanner;
public class Exercise06_15 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Enter ten numbers: ");

[code]....

View Replies View Related

Initializing Object Outputs Zeroes Instead Of Values Given To It

Oct 30, 2014

public class Check2
{
private int red;
private int green;
private int blue;

public Check2(){
red = 0;
green = 0;
blue = 0;

[Code] .....

And when i uses it in another class named "Check" it returns zeroes :

public class Check
{
public static void main(String[] args) {
Check2 obj = new Check2(125,254,12); // the toString method should return the values i gave it here . but it shows me zeroes instead.
System.out.println("the colors are " + obj.toString());
}
}

Output : the colors are (0,0,0)

View Replies View Related

Simple Function That Returns Array

Jun 1, 2014

Basically, it's to write a method that takes in, and then returns another array, whose first element is the average of the first two numbers, last element is the average of the last two, and then everything else is just the average of that index, the one before, and the one after. {1, 2, 4, 8} would return {1.5, 2.33333, 4.66666, 6}.I'm currently getting everything fine, except am not getting the last number (i.e. '6').

public class Arrays {
public static void main(String [] args){
double [] a1 = {1, 2, 4, 8};
for (int i = 0; i < a1.length; i++)
System.out.println(a1[i]);

[code]....

View Replies View Related

Create A Method That Returns A New Array

Mar 4, 2014

I need to create a method that returns a new array containing the componentwise sum of its arguments(if length is the same). For instance, if the input arrays are {0,1, 2} and {2, 2, 3} then the output is {0+2, 1+2, 2+3}, i.e. {2,3,5}.If the input arrays have different numbers of elements, the method should return null.

I came with something like this, however i dont know how to make a copy of an array from two arrays. My code obviously wont compile. package whatever;

import java.util.Arrays;
public class hhhh {
public static void main(String[] args) {
double [] a = {1,2,3};
double [] b = {2,3,4};

[code]...

View Replies View Related

Method That Returns Largest Value Of Array Of Doubles?

Jan 26, 2015

Write a Java method that returns the largest value of an array of doubles passed to the method as an argument.

Back into java wasn't sure how to do it for doubles did one in the main for integers and then added a method changed from int to double and now i'm lost as go why its not working.

package kickstarter9;
public class Kickstarter9 {
public static void main(String[] args){
double myList;
double[] myList = {6.0, 4.1, 2.4, 6.8, 1.9, 9.4, 2.8, 4.6, 9.3};
// find the largest value in the list

[Code]...

View Replies View Related

Method That Returns Largest Object In Array Of Objects

Apr 4, 2015

This is what I have to create : Write a method that returns the largest object in an array of objects. The method signature is:

public static Object max(java.lang.Comparable[] a)

All the objects are instances of the java.lang.Comparable interface. The order of the objects in the array is determined using the compareTo method.

Write a test program in the main method that creates an array of ten Strings, an array of ten Integers, and an array of ten java.util.Dates, and finds the largest string (i.e. in the alphabetical order), Integer, and Date in the arrays.

Name your java class Max and your java file Max.java.

I am struggling a bit with this code as I am sure you can see, and am at loss. I have never used the compareTo method. Am I doing this right, or on the right track with my code?

public class Max implements Comparable {
public static Object max(java.lang.Comparable[] a) {
java.lang.Comparable tempObj = null;
for (int i = 0; i < a.length; i++) {
if (a[i].compareTo(tempObj) > 0)

[Code] ....

View Replies View Related

Shuffling A Deck Of Cards

May 30, 2014

Assuming we have an array that is int[] cardDeck = new int[52] and each card is given a value with the following method; this deck does not include Jokers. Note: 11, 12, and 13 and used to represent Jacks, Queens, and Kings respectively, and 1 is used to represent Aces.

Java Code:

public static void loadDeck(int[] cardDeck)
{
for(int i = 0; i < cardDeck.length; i++)
{
if(i+1 > 39)
cardDeck[i] = i+1-39;
else if(i+1 > 26)

[Code] ....

View Replies View Related

Randomly Shuffling Cards

Apr 8, 2015

i'm trying to make a random card shuffler but the output would sometimes have same value multiple times. For example it might print out A5 at the fifth index then print out A5 again as the 32 index.

public class randomGenerateCards {
public static void main(String[] args) {
String temp;
String[] cards = new String[]
{ "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11",
"A12", "A13", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8",
"S9", "S10", "S11", "S12", "S13", "H1", "H2", "H3", "H4", "H5",
"H6", "H7", "H8", "H9", "H10", "H11", "H12", "H13", "D1", "D2",
"D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12",

[code]....

View Replies View Related

Program Returns 0 For All Calculations

May 28, 2014

this program works but returns zero for all the calculations. I checked everything there is no error but I dont know why is returning 0.

package mshproject3;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
public class HayloFactoryController {
public static void main(String[] args) {
String firstName = "";
String lastName = "";
String phoneNumber = "";
int aNbrOfVehicles = 0;
int aNbrOfFuelTanks = 0;
HayloFactory factory;

[code]....

View Replies View Related

Style Preferences For Conditional Returns?

Oct 21, 2014

I have a routine that returns a boolean. If any of a series of tests fails, the routine returns false, otherwise it returns true. For example, the routine might test for whether or not an integer is both odd and greater than 99, thus:

public boolean oddAndOld(int x)
{
if (x % 2 == 0)
return false;
if (x < 100)
return false;
return true;
}

I like the above because it suggests that "true" is the condition that applies if the incoming parameter meets all the required criteria. But I've sometimes seen this style used:

public boolean oddAndOld(int x)
{
if (x % 2 == 0)
return false;
if (x < 100)
return false;
else
return true;
}

I like this less because, among other things, if that last criterion is removed, the "else/return true" must be moved up into the immediately preceding test (or else leave some funny whitespace, depending on how you go about removing the departing "if" statement), but it does avoid suggesting that "return true" is hard-coded (that is, it reinforces that "true" is a conditional return value, not inevitable).

View Replies View Related

Make A ToString That Returns A Board

Jan 26, 2015

I've been trying to make a class that has a toString method that displays the board at the same time displays the value of each index of a 2d character array inside of the code. My professor has made the client print the method. I assumed since he was printing it on the client we had to return a string that gives us this.The number 2 is part of the 2d character array while the rest of the index values are just space characters.

___________
| | | |
|___|___|___|
| | | |
|___|___|___|
| | | |
|___|___|___|
| | | |
|___|___|___|
| 2 | | |
|___|___|___|

I had trouble figuring out how to return this so I started testing it out in another file.

public static void main(String[] args) {
// TODO Auto-generated method stub
char[][] myHidingPlaces;
myHidingPlaces = new char[5][3];
myHidingPlaces[0][0] = 'p';
char play = '1';
for (int i = myHidingPlaces.length-1; i >= 0 ; i--) {

[code]...

View Replies View Related

Variable Is Set Works / But Then In Void - Returns 0

Jun 13, 2014

The id variable is the problem Java Code: package com.cjburkey.games.boxee.objects;

import java.awt.Graphics;
import java.awt.Rectangle;
import com.cjburkey.games.boxee.GameState;
import com.cjburkey.games.boxee.resources.Images;
public class Block extends Rectangle {

[code]...

In the constructor, it returns corrent numbers, in the draw method, it returns 0. Why?

View Replies View Related

Function That Returns The Panel Of The Graph

Jun 9, 2014

Before I created the data file are the points of x and y I created a model class that represents the graph. I have a problem with making a function that returns the panel of the graph.

Attached File(s)

 data.cvs.txt (726bytes)
Number of downloads: 12
 chart.txt (3.72K)
Number of downloads: 10

View Replies View Related

Changing Canvas Returns With NullPointerException

Jun 21, 2014

I'm currently having trouble with a Breakout clone that I'm working on when I want to change the scenes of the game. When I start out with the main menu and I hit play, the main menu calls for the StateManager to remove the MainMenu and then to add the GameState to start playing through the levels. But, whenever this is done it just gives a NullPointerException and freezes the game. I know the code for initially starting a State is fine because that is the same way the MainMenu is loaded but there seems to be an issue with changing from one to another...

This is the StateManager class that adds and removes the States/Canvases to and from the JFrame/Main class:

public class StateManager
{
private ArrayList<State> states;
private int currentState;
private JFrame gameFrame;
public StateManager(JFrame gameFrame)
{
this.gameFrame = gameFrame;
states = new ArrayList<State>();

[Code] ....

I've tried messing around with the order of which things are added/loading from the StateManager but am just failing to see what I did wrong.

View Replies View Related

InputMismatchException GetMessage Returns Null

Nov 23, 2014

Working on chapter 10 of my book which covers exceptions and file I/O. The only part that does not work is line 40 where it is supposed to print getMessage(). It print's null instead of the more verbose error message.

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
public class SalesReport2
{
public static void main(String[] args)
{
String filename = "SalesData.txt";
int months = 0;

[Code] ....

temp.txt

123.33
asdf
549.85
dsaf
8456.23
21588.22
652.00

My output when using my test file.

ERROR: SalesData.txt does not exist.
Enter another filename: temp.txt
Nonnumeric data found in file: null
The invalid record will be skipped.
Nonnumeric data found in file: null
The invalid record will be skipped.
Number of months: 5
Total Sales: 31,369.63
Average Sales: 6,273.93

I then removed the try catch so that I could get a stacktrace

import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
public class SalesReport2a
{
public static void main(String[] args)

[Code] ...

This was the output.

ERROR: SalesData.txt does not exist.
Enter another filename: temp.txt
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at SalesReport2a.main(SalesReport2a.java:32)

I also pulled the copy of the code from the CD that came with the book and it's output is the same as my code.

The image in the book shows that the error should be more informative though.

what I get

Nonnumeric data found in file: null

vs

what I should get

Nonnumeric data found in file: For input string: "asdf"

Not sure why it isn't working.

View Replies View Related

Swing/AWT/SWT :: Changing Canvas Returns With NullPointerException

Jun 22, 2014

I'm currently having trouble with a Breakout clone that I'm working on when I want to change the scenes of the game. When I start out with the main menu and I hit play, the main menu calls for the StateManager to remove the MainMenu and then to add the GameState to start playing through the levels. But, whenever this is done it just gives a NullPointerException and freezes the game. I know the code for initially starting a State is fine because that is the same way the MainMenu is loaded but there seems to be an issue with changing from one to another...

This is the StateManager class that adds and removes the States/Canvases to and from the JFrame/Main class:
package com.pathtocreating.Breakout_V2.state;

import java.awt.BorderLayout;
import java.util.ArrayList;
import javax.swing.JFrame;
public class StateManager {
private ArrayList<State> states;

[Code] ....

The code that calls the change of state in MainMenuState is called in the update method:

if(inputController.isMouseClicked("LeftClick")) {
if(playHover) {
soundManager.playSound("ButtonClick");
BreakoutMain.sm.changeStates("GameState");

[Code] ....

Here is the full stack trace when clicking the area to change states:

Exception in thread "Thread-2" java.lang.NullPointerException
at sun.java2d.SunGraphics2D.validateColor(Unknown Source)
at sun.java2d.SunGraphics2D.<init>(Unknown Source)
at sun.awt.image.SunVolatileImage.createGraphics(Unknown Source)

[Code] ...

It gives that same value both before and after clicking on the region.

Printing out b after the State is changed to GameState gives the following:

java.awt.Component$BltSubRegionBufferStrategy@66116674

I've tried messing around with the order of which things are added/loading from the StateManager but am just failing to see what I did wrong.

View Replies View Related

String Declaration - Check A / B Returns False

Jan 30, 2014

I have doubts in string declaration. As I know we can declare string in two ways:

1. String a=new String("Hello");
2. String b="Hello";

What is exact difference between them? Another thing is when I check (a==b) it retuns me false, but when I check a.equals(b) it returns me with true. Why So?

View Replies View Related

Removing Line Feeds Or Carriage Returns

Jun 11, 2014

I have a requirement where I need to read a file with so many carriage return line feeds. Attached is the file I am reading.

I have written a sample application to read the file and and remove these new line feeds but when I execute it, I see the line feed.

import java.io.*; 
public class Main { 
public static void main(String[] args) throws IOException{

[Code].....

Is there away to achieve this in attached output file?

This is just one record, but I have several records like these.

View Replies View Related

Calculate Nth Fibonacci Number And Returns That To User

May 1, 2014

I have to create a program that calculates the nth Fibonacci number and returns that to the user. Fibonacci said his number sequence would describe the ideal breeding patterns of immortal rabbits. So, you are going to make this vision a reality.

First, take in a numeric value from the user and calculate that value in the fibonacci series. Next, find an image of a rabbit and display the image on a GUI (put the image as an icon on a label!) the number of times returned by the algorithm (Put all the aforementioned labels on one panel with FlowLayout!).

You need to remove the old images from the Panel. Probably the easiest way to do this is to create a whole new panel and remove the old one (hint: the remove method of JPanel should come in handy)

You could use an array of JLabels
You will need to create a new JLabel and add each member of the array to the panel
I should be able to scroll to see any images that are off screen

I am having difficultly on to making the array list for JLabel, and getting the Fibonacci sequence to show the pictures of rabbits. Below is my current code.

import java.awt.FlowLayout;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Rabbit extends JFrame

[Code] ....

View Replies View Related

Queues - IsEmpty Method Only Returns False

Jan 29, 2015

My isEmpty method only returns false. Is something wrong? I printed the empty and not empty for testing purposes.

//determines if there are any items in the queue
public boolean isEmpty() {
if (front == -1 && rear == -1) {
System.out.println("empty");
return true;
} else {
System.out.println("not empty");
return false
}
}

View Replies View Related

Static Method That Requires No Arguments And Returns No Value

Sep 30, 2014

I am doing a homework assignment and I am clueless as to what I did wrong or supposed to do in terms of placement of coding. If someone could dumb down their response and tell me what I did wrong

This is what I supposed to do:

1.) Type the following shell for the class:

public class ParadiseInfo
{
}

2.) Between curly braces of the class, indent a few spaces and create the shell for the main() method:

public static void main(String[] args)
{
}

3.) Between the braces of the main()Method, insert a call to the displayInfo() method:

displayInfo();

4.) Place the displayInfo() method outside of the main() method, just before the closing curly brace for the ParadiseInfo class:

public static void displayInfo()
{
system.out.println ("Paradise Day Spa wants to pamper you.");
system.out.println ("We will make you look good.");
}

This is what I attempted to do: I know it is wrong I am just clueless on where to put the code and why

public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
public static void displayInfo();
}
system.out.println("Paradise Day Spa wants to pamper you.");
system.out.println("We will make you look good.");
}

--- Update ---

I have also tried this:

public class ParadiseInfo
{
displayInfo();
public static void main(String[] args)
{
}
public static void displayInfo();
{

[Code]...

The very last attempt I only ended up with one error which is:

G:ParadiseInfo.java:3: error: invalid method declaration; return type required
displayInfo();
^
1 error

Tool completed with exit code 1

Java is very new to me and I have no clue on what I am doing wrong.

View Replies View Related

Alert Returns Added String Instead Of Adding Var Together

Jan 28, 2014

The following alert returns an added string instead of adding the var together to give me a total?

var tvHours = prompt('How many hours of Tv do you watch last week?');
var webHours = prompt('How many hours of internet surfing did you do last week?');
var gameHours = prompt('How many hours of video games did you play last week?');
alert(tvHours + webHours + gameHours);

Also can you store a var in an alert? e.g ( var time = alert(tvHours + webHours + gameHours); ???

View Replies View Related

Using Method Calls With Returns But It Keeps On Showing Errors

Oct 12, 2014

I am trying to use method calls with returns but it keeps on showing errors. The errors say class, interface, or enum expected. I realize this error occurs if there is issue with declaring class - but i can't seem to find the error. I will post the code that shows error.
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
public class FuelCost extends JFrame
{
// declarations
Color black = new Color(0, 0, 0);

[Code] ....

The error states:

C:UsersPriti BhattaraiDesktopCCAC JavaFuelCost.java:290: error: ';' expected
public double calculateCostToFillTank()
^
C:UsersPriti BhattaraiDesktopCCAC JavaFuelCost.java:292: error: class, interface, or enum expected
return (averageMilesPerTank / milesPerGallonRating) * currentPricePerGallon;

[Code] ....

12 errors

Tool completed with exit code 1

View Replies View Related







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