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?
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++) {
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: ");
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()); } }
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]);
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};
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
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)
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)
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.
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).
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.
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--) {
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
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.
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"
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:
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:
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?
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
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 } }
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:
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); ???
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);