Why is my code constantly giving the same random numbers - random

Whenever I execute the program it always prints out 9 and 5 for sc1 and sc2. It was my understanding that the random class was supposed to be mostly random.
Here is my code:
public class BlackJack {
public static BlackJack blackjack;
public int chips;
public static int[] deck;
public static int ct = 0, sc1, sc2;
Random random;
public BlackJack() {
deck();
deal();
System.out.println(sc1);
System.out.println(sc2);
}
public void deck() {
deck = new int[52];
for (int i = 0; i < 52; i++) {
if(i % 4 == 0) {
ct++;
}
deck[i] = ct;
}
}
public void deal() {
random = new Random(52);
sc1 = deck[random.nextInt(52)];
sc2 = deck[random.nextInt(52)];
}
public static void main(String[] args) {
blackjack = new BlackJack();
}
}
Thanks in advance for any guidance.

Never mind. i'm dumb. It was the fact that i had random = new Random(52); instead of random = new Random();

Related

how to make a loop of the amount of space in a train

I want to make a train and at every stop is says how many people are coming in and going out, and if the maximum is reached, then it declines the rest. I also have seated spots and standing spots and if there are any seated spots left, the standing people go sit instead of stand.
so here is what I have
public class Train {
private int declinedPassengers; // instance variables
private int passengerInTrain;
private int numberOfSeats;
private int numberOfStandingSpots;
private int numberOfBoardingPassengers;
private int numberOfAlightingPassengers;
private int numberOfSeatedPassengers;
private int numberOfStandingPassengers;
private int numberOfDeclinedPassengers;
public static void main(String[] args){
Train train = new Train(150, 100);
train.stopAtStation(100, 0);
train.stopAtStation(75, 10);
System.out.println("After two stops:");
System.out.println(train.getNumberOfSeatedPassengers());
System.out.println(train.getNumberOfStandingPassengers());
train.stopAtStation(100, 0);
train.stopAtStation(100, 10);
System.out.println("After four stops:");
System.out.println(train.isFull());
System.out.println(train.getNumberOfSeatedPassengers());
System.out.println(train.getNumberOfStandingPassengers());
System.out.println(train.getNumberOfDeclinedPassengers());
train.stopAtStation(10, 50);
System.out.println("After five stops:");
System.out.println(train.isFull());
System.out.println(train.getNumberOfSeatedPassengers());
System.out.println(train.getNumberOfStandingPassengers());
System.out.println(train.getNumberOfDeclinedPassengers());
}
public Train(int numberOfSeats, int numberOfStandingSpots) {
for(int i = 0; i <= numberOfSeats; i++){
}
}
public void stopAtStation(int numberOfBoardingPassengers, int numberOfAlightingPassengers) {
}
public int getNumberOfSeatedPassengers() {
return numberOfSeatedPassengers;
}
public int getNumberOfStandingPassengers() {
return numberOfStandingPassengers;
}
public int getNumberOfDeclinedPassengers(){
return numberOfDeclinedPassengers;
}
public boolean isFull(){
int seated = getNumberOfSeatedPassengers();
int standing = getNumberOfStandingPassengers();
if(seated == 150 && standing == 100){
return true;
}
else{
return false;
}
}
}
and I am stuck at the for loop on how to make a loop such that it counts down the amount of seats, or up the amount of seated/ standing people

I am trying to print the method findAverage in the main method, can anyone tell me how to fix

public class Grade {
private int [] array = {2,3,1,4,5,7,1};
public int findSum() {
int sum;
sum = 0;
for(int i =0; i <array.length; i++)
{
sum = sum +array[i];
}
return sum;
}
public double findAverage() {
double average;
average = findSum()/array.length;
return average;
}
}
class ExamClient {
public static void main(String[] args) {
double answer;
answer = findAverage();
System.out.println("Average of all elements in the array is" + answer);
}
}
In the main you have to create a instance of the class
Create instance
public static void main(String[] args)
{
double answer;
Grade g= new Grade();
answer = g.findAverage();
System.out.println("Average of all elements in the array is" + answer);
}
Also you can make the method static

JUnit testing GUI class

I've looked over the stackoverflow and the internet and I couldn't find a clear answer that helped me.
I have an assignment and it includes the following class, which is a GUI. I have Junit tested the other classes but for this I didn't know how.
import java.awt.*;
public class CruiseDisplay extends Canvas {
private int recorded = 0; //recorded speed
private boolean cruiseOn = false; //cruise control state
private final static int botY = 200;
private Font small = new Font("Helvetica",Font.BOLD,14);
private Font big = new Font("Helvetica",Font.BOLD,18);
public CruiseDisplay() {
super();
setSize(150,260);
}
Image offscreen;
Dimension offscreensize;
Graphics offgraphics;
public void backdrop() {
Dimension d = getSize();
if ((offscreen == null) || (d.width != offscreensize.width)
|| (d.height != offscreensize.height)) {
offscreen = createImage(d.width, d.height);
offscreensize = d;
offgraphics = offscreen.getGraphics();
offgraphics.setFont(small);
}
offgraphics.setColor(Color.black);
offgraphics.fillRect(0, 0, getSize().width, getSize().height);
offgraphics.setColor(Color.white);
offgraphics.drawRect(5,10,getSize().width-15,getSize().height-40);
offgraphics.setColor(Color.blue);
offgraphics.fillRect(6,11,getSize().width-17,getSize().height-42);
}
public void paint(Graphics g) {
update(g);
}
public void update(Graphics g) {
backdrop();
// display recorded speed
offgraphics.setColor(Color.white);
offgraphics.setFont(big);
offgraphics.drawString("Cruise Control",10,35);
offgraphics.setFont(small);
drawRecorded(offgraphics,20,80,recorded);
if (cruiseOn)
offgraphics.drawString("Enabled",20,botY+15);
else
offgraphics.drawString("Disabled",20,botY+15);
if (cruiseOn)
offgraphics.setColor(Color.green);
else
offgraphics.setColor(Color.red);
offgraphics.fillArc(90,botY,20,20,0,360);
g.drawImage(offscreen, 0, 0, null);
}
public void drawRecorded(Graphics g, int x, int y, int speed) {
g.drawString("Cruise Speed",x,y+10);
g.drawRect(x+20,y+20,50,20);
g.setFont(big);
g.drawString(String.valueOf(speed+20),x+30,y+37);
g.setFont(small);
}
public void enabled() {
cruiseOn = true;
repaint();
}
public void disabled() {
cruiseOn = false;
repaint();
}
public void record(int speed) {
recorded=speed;
repaint();
}
}
Can somebody help me please?

Simple animation through use of paintComponent

I'm trying to make a small square move across the top of the panel. I'm not worried about the seamlessness of the animation or flicker or anything like that. It appears that in the while-loop, repaint() isn't repeatedly calling the paintComponent. Thoughts?
public class NodeMove extends JFrame {
boolean running = true;
public NodeMove() {
widgetNode panel = new widgetNode();
add(panel);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
Runnable node = new widgetNode();
Thread thread1 = new Thread(node);
thread1.start();
}
class widgetNode extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private int x = 30;
private int y = 30;
public widgetNode() {
}
public void run(){
while(running){
nodeUpdate();
repaint();
try {
Thread.sleep(500);
} catch (InterruptedException e) {}
}
}
public void nodeUpdate(){
x += 4;
}
protected void paintComponent(Graphics g) {
super.paintComponents(g);
g.drawRect(x, y, 30, 30);
}
}
public static void main(String[] args) {
NodeMove frame = new NodeMove();
for(int i = 0; i < 50; i++){
frame.repaint();
}
}
}

Why different result each run?

I'm playing around with CompletableFuture and streams in Java 8 and I get different printouts each time I run this. Just curious, why?
public class DoIt {
public static class My {
private long cur = 0;
public long next() {
return cur++;
}
}
public static long add() {
long sum = 0;
for (long i=0; i<=100;i++) {
sum += i;
}
return sum;
}
public static long getResult(CompletableFuture<Long> f) {
long l = 0;
try {
f.complete(42l);
l = f.get();
System.out.println(l);
} catch (Exception e) {
//...
}
return l;
}
public static void main(String[] args){
ExecutorService exec = Executors.newFixedThreadPool(2);
My my = new My();
long sum = Stream.generate(my::next).limit(20000).
map(x -> CompletableFuture.supplyAsync(()-> add(), exec)).
mapToLong(f->getResult(f)).sum();
System.out.println(sum);
exec.shutdown();
}
}
If I skip the f.complete(42l) call I always get the same result.
http://download.java.net/jdk8/docs/api/java/util/concurrent/CompletableFuture.html#complete-T-
by the time the complete(42l) call happens, some add()'s may have already completed.

Resources