Off-by-one Error In Perfect Shuffle Method

Jan 29, 2015

I am supposed to create a method that simulates a perfect shuffle of a given array of integers. According to the assignmment, this is what a perfect shuffle is:

This procedure is called a perfect shuffle if the interleaving alternates between the two half-decks.Unfortunately, the perfect shuffle comes nowhere near generating all possible deck permutations.In fact, eight shuffles of a 52-card deck return the deck to its original state!Consider the following perfect shuffle algorithm that starts with an array named cards that contains 52 cards and creates an array named shuffled.

Initialize shuffled to contain 52 empty elements.
Set k to 0.
For j = 0 to 25,
− Copy cards[j] to shuffled[k];
− Set k to k+2.
Set k to 1.
For j = 26 to 51,
− Copy cards[j] to shuffled[k];
− Set k to k+2.

This approach moves the first half of cards to the even index positions of shuffled, and it moves the second half of cards to the odd index positions of shuffled.The above algorithm shuffles 52 cards. If an odd number of cards is shuffled, the array shuffled has one more even-indexed position than odd-indexed positions. Therefore, the first loop must copy one more card than the second loop does. This requires rounding up when calculating the index of the middle of the deck. In other words, in the first loop j must go up to (cards.length + 1) / 2, exclusive, and in the second loop j most begin at (cards.length + 1) / 2.

View Replies


ADVERTISEMENT

Determine All Of Perfect Squares Between 1 And 100 - Precision Error?

Mar 26, 2015

I am teaching myself Java and am trying to write a function that will determine all of the perfect squares between 1 and 100 but am running into a problem...

Here's my code:

package sqrroot;

public class SqrRoot {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double sroot, rerr;
int count = 0;
for(double num = 1.0; num <= 100.0; num++){

[Code] ....

and here is the output:

run:
0.0
1.0 is a perfect square.
0.0
4.0 is a perfect square.

[Code] ....

There are 49 perfect squares between 1 and 100.
BUILD SUCCESSFUL (total time: 6 seconds)

Which is clearly wrong. Is there something wrong with my code or is this due to inherent imprecision in the double type or the Math.sqrt function?

View Replies View Related

Checking For Perfect Numbers

Feb 10, 2015

I'm trying to write code for a program that checks numbers upto a number that the user gives. I've looked around, and the nearest matches I can see are people using for loops with predetermined amounts of looping.The only real tools I'm allowed to use are basic arithmetic, and while loops. The program is supposed to have 2 nested while loops.

import java.util.Scanner;
import java.io.*;
public class questionTwo234234 {
public static void main(String[] args){

[code]...

View Replies View Related

Perfect Number Program

Oct 28, 2014

import java.util.Scanner;
public class pdonahue_Perfect
{
public static void main (String args[])
{
int test;
int value=0;
Scanner input = new Scanner(System.in);

[code]....

i have just a couple of issues when running my program.
-First, When i enter 0 it should print..

Please enter a possible perfect number: 0
0:NOT PERFECT

-Second, when i get a perfect number. it should show the answer in descending order..

Please enter a possible perfect number: 6
6:3 2 1

mine shows: Please enter a possible perfect number: 6 6: 1 2 3

**everything else compiles correctly with no errors

View Replies View Related

How To Shuffle Numbers Every New Game

Dec 15, 2014

How to shuffle the numbers every new game? Heres my the code.

package project;
import java.io.*;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class khoProject {

[Code] ....

View Replies View Related

How To Shuffle Array Using Random Numbers

Nov 10, 2014

For a project we have to "shuffle" items in an array using random numbers. We are supposed to generate random numbers and use those numbers to exchange array elements. But I am not sure what that means, "exchange array elements". Does that mean you generate 2 random numbers within the length of the array, and then switch the items at those locations in the array?

View Replies View Related

Algebra - Perfect Square Java Program / Find Smaller Integer N

Sep 22, 2014

Write a program that prompts the user to enter an integer m and find the smallest integer n such that m * n is a perfect square. (Hint: Store all smallest factors of m into an array list. n is the product of the factors that appear an odd number of times in the array list. For example, consider m = 90, store the factors 2, 3, 3, 5 in an array list. 2 and 5 appear an odd number of time in the array list. So, n is 10.)

so far my program is just like this.

import java.lang.Math;
import java.util.Scanner;
public class PerfectSquare {
public static void main(String[] args) {
Scanner m = new Scanner(System.in);
int Fint;

[Code] .....

how do i make the program find the smallest integer n?

View Replies View Related

Applets :: Display Images Of Playing Cards And Shuffle The Deck

Jul 28, 2014

"In this assignment you will use an applet to display images of playing cards. The applet should load a deck of 52 playing card images from the "images" folder that you downloaded. The applet should shuffle the deck (use a random number generator) and display the first 10 cards of the shuffled deck. Display the cards in two rows of five cards each."

That is my goal for this assignment. I've got my code compiling and I will post it below and I've got an html page but when I try to open it I get an error

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;
public class Assignment12 extends Applet {
Image card1;
Image card2;

[Code] ....

View Replies View Related

Random Shuffle Array Of Card Objects Which Does Function Of Deck

Oct 26, 2014

I have to random shuffle an array of Card Objects which does the funcion of a deck. Heres the code:

Java Code:

public void barajear(){
int j;
for (int i=0;i<52;i++){
j=Baraja.random(51);
if (this.mazo[j]==null){
this.mazo[j]=this.arreglo[i];
}else{
--i;
}
}
} mh_sh_highlight_all('java');

so bassically theres an array called "arreglo" which has the cards in order and the function "random" its an rng of numbers from 0 to 51.what i'm trying to do it's to take the cards from the ordenated array and put them randomly in the other but only if it's empty.(the array "mazo" has alredy been initialized with null).it worked at first, but now, after compiling succesfully i tried to run it and the cmd just...

View Replies View Related

Images Of Cards - Shuffle Deck With Random Number Generator And Display First 10

Aug 18, 2014

So I'm trying to make an applet that displays images of cards. The applet has a deck of 52 playing card images. It should shuffle the deck with a random number generator and display the first 10 cards of the shuffled deck.

With this code I have about 100 errors and I'm not sure what I'm doing wrong: I know I have to use arrays in order to ultimately display the images, but do I have to create Image objects for the Image [] cards array? I am getting many "class, interface, enum expected" errors as well as others.

import java.util.Random;
import java.awt.Image;
import java.applet.Applet;
import java.awt.Graphics;
public class unit12 extends Applet
{

[Code] .....

View Replies View Related

LinkedList No Such Method Error

Oct 27, 2014

Okay, I'm having a problem with my LinkedList. When I run the driver program it's telling me that I have no "add" method and I don't know what's going on.

Driver Program:

public class TestLinkedList {
public static void main(String[] args)
{
MyLinkedList<String> L = new MyLinkedList<String>();
L.add("Browns");
L.add("Ravens");
L.add("Steelers");

[code]....

View Replies View Related

Getting Error On Equals Method For Objects

Mar 26, 2014

I have to create a class that has two fields. One called length and the other width. I have to make a method that returns the tract area. Similarly, I also have to make a method that indicates whether two objects have the same fields. Here is the code that I have assembled...so far

// create private fields to hold width and length
private double width;
private double length;

[Code].....

My problem is encountered when writing that equals method

if(length.equals(object.length) && width.equals(object.width))

I get an error saying HTML Code: cannot invoke equals(double) on the primitive type double. Meanwhile, I do see, to realize that when I change my fields to capital "Double." The problem disappears; however, in my class I have never dealt with a situation where I have to use capital d in double. In fact, I don't even know what's the difference between Double and double. I do know what double is but not the other one..

View Replies View Related

JSP :: Calling Java Method Error

Apr 13, 2014

I have a JSP page that calls a Java method .. using GlassFish 4.0 it worked just fine, now I'm trying to run it on a new server with Tomcat 6.0 but it keeps giving me this error: "the function result must be used with a prefix when a default namespace is not specified"

Here's my JSP page:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
......
<jsp:useBean id="diskUtilData" scope="request" class="newpackage.ResultPage" />
.....
<c:forEach var="celldata" items="${diskUtilData.result()}">

[Code].....

View Replies View Related

Error When Trying To Return Array Via Method

Jan 14, 2014

I am getting these errors with this code, I can not figure why this error is occuring.

what is the issue with this code?

Java Code: package ABC;
import java.util.ArrayList;
import java.util.Scanner;
public class GetInputFromUser {
private ArrayList<String> name = new ArrayList<String>();

[code]....

View Replies View Related

Void Method Recursion Error

Jan 11, 2015

i'm trying to exit the recursion of a void method but some funny things are happening before and after the return statement.

public static void main(String args[])
{
//code
rec(x,y);
System.out.println("HEllooooooo");}
public static void rec(int x,int y)
{
try
{
System.out.println(x+" "+y+" "+check);
if(x==fx && y==fy)

[code]....

View Replies View Related

Cannot Find Method Append Error

Mar 24, 2014

I am trying to run the following code but getting the error above.
 
import java.io.*;
public class StringBuilder {
    public StringBuilder() {
        super();
    }   
    public static void main(String args[]) {
            // creates empty builder, capacity 16
            StringBuilder sb = new StringBuilder();
            // adds 9 character string at beginning
            sb.append("Greetings");
    }
}

View Replies View Related

Method Is Not Returning Array - Null Error

May 18, 2015

I am trying to return an array and I keep getting a null error. The below class sets the material numbers into an array and should return that array if called :

public class Jobs {
private int[] materialsNumber;
//change to parts and create another class that gets the materials for the parts
 public int[] job1() {
materialsNumber[0] = 11960120;

[Code] ....

I later try to call the method. The program executes but stops after I println "test in loop"

public class PurchaseOrdersToParts {
 private Jobs job = new Jobs();
 int[] getPartsForPurchaseOrder(BigDecimal purchaseOrder) {
 System.out.println("inside getparts");
 BigDecimal testNum = new BigDecimal(123.0);
 
[Code] ....

This is the method that is calling the method in the GenerateOrdersToParts class

private PurchaseOrdersToParts purchaseOrdersToParts = new PurchaseOrdersToParts();
 @Inject
PoRepository poRepository;
public GenerateShopJobTickets() {
 
[Code] .....

View Replies View Related

Error In Main Method - Cannot Find Symbol

Dec 15, 2014

I am writing a palindrome program. I don't understand what is wrong with my Main method. It is giving me error and error is "Can not find symbol in main method"

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PalindromeA extends JFrame {
private JTextField inText;
private JTextField outText;

[Code] ....

View Replies View Related

Comparator - No Suitable Method Found Error

Apr 24, 2014

So I built this comparator exactly the same way I built my others that are working.But the comparator for UserComparator is not being found for some reason.I will post my usercomparator class and JSP page.

<%@page import="tickets.UserComparator"%>
<%@page import="tickets.User"%>
<%@page import="tickets.ReporterTTComparator"%>
<%@page import="tickets.StatusTTComparator"%>
<%@page import="tickets.SystemTTComparator"%>
<%@page import="java.util.Collections"%>
<%@page import="java.util.ArrayList"%>
<%@page import="tickets.TroubleTicket"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>

[code]....

View Replies View Related

Null Point Exception Error While Calling A Method

Mar 19, 2015

I'm trying to call the grade.processFile method from the main method but I'm getting this Error below. I'll post my code which includes the main method and the class underneath the error message:

Exception in thread "main" java.lang.NullPointerException
at java.io.FileInputStream.<init>(FileInputStream.jav a:130)
at java.util.Scanner.<init>(Scanner.java:611)
at MyGrades.processFile(MyGrades.java:49)
at myGradesMain.main(myGradesMain.java:19) 
import java.util.Scanner;
import java.io.*;

[code]...

View Replies View Related

Hangman Program - No Suitable Method Found Error

Apr 7, 2015

I am trying to create a Hangman program using Arrays. When trying to compile my code I get the following error: 50: error: no suitable method found

for setCharAt(int,String)
method StringBuffer.setCharAt(int,char) is not applicable
method AbstractStringBuilder.setCharAt(int,char) is not applicable

Here's the code:

import java.util.*;
import java.io.*;
public class hangman {
public static void main() throws IOException {
Scanner kb = new Scanner(System.in);
String filename;

[code]....

View Replies View Related

N-Sided Regular Polygon Main Method Error

Sep 5, 2014

My code is giving me an error at the main method and it says that modifier 'static' is not allowed in constant variable declarations. every program that i searched for had the same code line but none had the problem i do.

package regularpolygon;
/**
*
* @author home1
*/
import java.lang.Math;
import java.text.*;

[code]...

View Replies View Related

IOexception Expected Error Action Listener Method

May 9, 2015

Need to write two files but getting an expected exception error.
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Scanner;
import java.io.FileWriter;
public class TestingPanel extends JPanel

[Code] .... 
 
TestingPanel.java:49: error: unreported exception IOException; must be caught or declared to be thrown
FileWriter outputFileQuestions = new FileWriter("Test.txt");
^
TestingPanel.java:50: error: unreported exception IOException; must be caught or declared to be thrown
FileWriter outputFileAnswers = new FileWriter("Answers.txt");

[Code] ....

View Replies View Related

Getting Error While Creating A Method - Void Is Invalid Type For Variable

Jun 25, 2014

I am new to Java and trying to learn it.I wrote the following program but while creating the method nav i am getting errors.

Error:- void is an invalid type for the variable nav

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.*;
public class YahooHomepage {
private static WebDriver driver;

[Code] .....

View Replies View Related

How To Solve No Suitable Method Found For Write (String) Error

Sep 1, 2014

I wrote this program to prompt user to enter his choice to do a i/o operation in a file. It shows error. How to clear the error. My code is:

import java.io.*;
import java.util.*;
class Files {
public static void main(String args[]) {
String n;

[Code] ....

error:
E:java>javac Files.java
Files.java:26: error: no suitable method found for write(String)
fos.write(n);

[Code] ....

View Replies View Related

How To Change State Of Object - Main Method Exception Error

Jan 8, 2015

The error I get when I execute my java file.

Exception in thread "main" java.lang.NullPointerExceptionat DogTestDrive.main(DogTestDrive.java:19)

Here is the source

class Dog {
int size;
String name;
void bark () {
if (size < 60) {
System.out.println("Woof woof");

[code]....

Some background: I'm reading "Head first Java 2nd edition" and I'm going through the examples which is showing me how to change the state of an object. The original code looks like the code below, however the previous chapter went over creating array's of an object, so I created an array of the object "Dog" and wanted to re-write it this way. To my understanding, it should work but it's giving me that error when I execute it. The error itself isn't very clear, if I could get a line number pointed to, that would work.

class Dog {
int size;
String name;
void bark() {
if (size > 60) {
System.out.println(“Wooof! Wooof!”);
} else if (size > 14) {
System.out.println(“Ruff! Ruff!”);
} else {
System.out.println(“Yip! Yip!”);

[code]....

View Replies View Related







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