Trying to sort an ArrayList of value - ajax

I am attempting to sort an ArrayList in my program which has the functionality of storing different rugby teams and there results in an ArrayList and printing them in the form of a table. I have managed to do everything but the sorting. This is my code so far.
package javaCode.RugbyCompetitionTable;
import java.util.Collections;
public class Team{
private String teamName;
private int won;
private int drew;
private int lost;
private int scored;
private int conceded;
private int scoreDifference;
private int triesScored;
private int triesAgainst;
public Team(String teamName) {
this.teamName = teamName;
}
public String getTeamName() {
return teamName;
}
public int getWon() {
return won;
}
public int getDrew() {
return drew;
}
public int getLost() {
return lost;
}
public int getScored() {
return scored;
}
public int getConceded() {
return conceded;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public int getScoreDifference() {
return scoreDifference;
}

You're sorting based upon which teams beat one another. You presumably want to sort based on the number of points:
#Override
public int compareTo (Team team) {
if (team.getScoreDifference() > this.getScoreDifference()) {
return 1;
}
else if (team.getScoreDifference() < this.getScoreDifference()) {
return -1;
}
else {
return 0;
}
}
Sample output:
Leeds Rhinos 2 0 0 12 5 7
Sheffield 1 0 0 15 13 2
London 0 1 0 13 13 0
Burnley 0 1 0 10 10 0
Doncaster 0 0 1 12 20 -8
Southampton 0 0 1 10 20 -10
You can actually rewrite the compareTo much more simply:
#Override
public int compareTo(Team team) {
return Integer.compare(team.getScoreDifference(), this.getScoreDifference());
}

Related

Blank return value( sum of two numbers)

public class Sum {
public static void main(String[] args) {
add(19, 21);
}
public static int add(int number1, int number2) {
int sum = number1 + number2;
return sum;
}
}
Why is not returning the value 40 from the sum and is there a better way to write this method.

how to use Java 8 for long function definitions?

I have a an object Product,
and code as below , hashSetProducts is LinkedHashSet of Products. How can I write all below using Java 8 stream function ? I understand that value of remianing will be replaced each time. I want the final value after the for loop exits.
int getRemaining(int remaining){
for(Product P : hashSetProducts){
remaining = calculate(p.qty(), p.price(), remaining, location); //
use Java 8 stream here
}
return remaining
}
private int calculate(int qty, double price, int rem, Location location){
if(rem== 0){
return 0;
}
int avail = location.get(qty, rem);
if(avail > 0){
rem = avail - rem;
}
return rem;
}
mapToLong will execute arbitrary code that returns a long. Here is an MCVE that uses your calculation verbatim:
import java.util.LinkedHashSet;
public class HelloWorld{
public static class Product {
private int qty;
private double price;
private int used;
public Product(int qty, double price, int used) {
this.qty = qty;
this.price =price;
this.used = used;
}
public int qty() {return qty;}
public double price() {return price;}
public int used() {return used;}
};
public static class Location {
public long get(int qty, int used) { return 0; };
};
public static void main(String []args) {
LinkedHashSet<Product> hashSetProducts = new LinkedHashSet();
hashSetProducts.add(new Product(1,1.0,1));
hashSetProducts.add(new Product(2,2.0,2));
hashSetProducts.add(new Product(3,3.0,3));
Location location = new Location();
long remaining = hashSetProducts.stream().mapToLong(p -> {
int qty = p.qty();
int used = p.used();
if( used == 0 )
return 0;
long rem = location.get(qty, used);
if( qty > 0)
rem = used - rem;
return rem;
}).sum();
System.out.println(remaining);
}
}

Why is my code constantly giving the same random numbers

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();

use an int Variable for range to get random number

I am still fairly new to java. I want to make a game with 3 character types that have different stats. I am using int values for each type so that their attack value is a range instead of just being a constant value. Since each character has a different range, I want to substitute an int value instead of an actual number for the method to get a random number. Here is my code.
package battleme;
import java.util.Random;
/**
*
* #author Kitten
*/
class Character {
String name;
int life;
int playerAttacklow;
int playerAttackhigh;
int playerDefense;
int playerLevel;
int currentXP;
int currentGold;
public Character(String name, int life, int playerAttacklow,
int playerAttachhigh, int playerDefense,
int playerLevel, int currentXP, int currentGold) {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
public int getPlayerAttacklow() {
return playerAttacklow;
}
public void setPlayerAttacklow(int playerAttacklow) {
this.playerAttacklow = playerAttacklow;
}
public int getPlayerAttackhigh() {
return playerAttackhigh;
}
public void setPlayerAttackhigh(int playerAttackhigh) {
this.playerAttackhigh = playerAttackhigh;
}
public int getPlayerDefense() {
return playerDefense;
}
public void setPlayerDefense(int playerDefense) {
this.playerDefense = playerDefense;
}
public int getPlayerLevel() {
return playerLevel;
}
public void setPlayerLevel(int playerLevel) {
this.playerLevel = playerLevel;
}
public int getCurrentXP() {
return currentXP;
}
public void setCurrentXP(int currentXP) {
this.currentXP = currentXP;
}
public int getCurrentGold() {
return currentGold;
}
public void setCurrentGold(int currentGold) {
this.currentGold = currentGold;
}
//the problem child
int ActualAttackGen(int playerAttackhigh, int playerAttacklow) {
Random rn = new Random();
int randomNum;
randomNum= rn.nextInt((playerAttackhigh-playerAttacklow) + 1)+ playerAttacklow ;
return randomNum ;
}
package battleme;
public class BattleMe {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Character Warrior = new Character("Warrior", 30, 2, 10, 3, 1, 1, 15);
Character Rouge = new Character("Rouge", 25, 3, 6, 2, 1, 1, 15);
Character Mage = new Character("Mage", 18, 2, 8, 1, 1, 1, 15);
// trying to run the problem child
System.out.println(Warrior.ActualAttackGen(Warrior.playerAttackhigh,Warrior.playerAttacklow));
}
}
Whenever I try to run this, I always get an value of 0. Please help!
In your constructor you have to assign the passed values to the respective member of your Character class:
public Character(String name, int life, int playerAttacklow,
int playerAttachhigh, int playerDefense,
int playerLevel, int currentXP, int currentGold) {
this.name = name;
....
}
BTW: It would be good coding practice to distinguish member and parameter names. Usually one prefixes one of them (or both). E.g. member myName, parameter aName. So you do not have to reference the member with "this.":
myName = aName;

get average value from a tree of nodes

I have to implement this method:
public int GetAverage(Node root){
//TODO implement
}
this method should get average value of all nodes of root tree. where :
public interface Node {
int getValue();
List<Node> getNodes();
}
do you have any ideas how to implement this method?
thank you
my attempt:
public static double value;
public static int count;
public static double getAverage(Node root) {
count++;
value += root.getValue();
for (Node node : root.getNodes()) {
getAverage(node);
}
return value / count;
}
but how to do it without the static fields outside of the method?
Simply traverse through all nodes and remember the count and the overall sum of all values. Then calculate the average. This is an example written in Java.
public interface INode {
int getValue();
List<INode> getNodes();
}
public class Node implements INode {
private List<INode> children = new ArrayList<INode>();
private int value;
#Override
public int getValue() {
return value;
}
#Override
public List<INode> getNodes() {
return children;
}
public static int getAverage(INode root) {
if (root == null)
return 0;
Counter c = new Counter();
calculateAverage(root, c);
return c.sum / c.count;
}
class Counter {
public int sum;
public int count;
}
private static void calculateAverage(INode root, Counter counter) {
if (root == null)
return;
counter.sum += root.getValue();
counter.count++;
// recursively through all children
for (INode child : root.getNodes())
calculateAverage(child, counter);
}
}
public static double getAverage(Node root) {
Pair p = new Pair(0,0);
algo(root, p);
return ((double) p.element1) / ((double) p.element2);
}
private static void algo(Node root, Pair acc) {
for(Node child : root.getNodes()) {
algo(child, acc);
}
acc.sum += root.getValue();
acc.nbNodes++;
}
With Pair defined as follows:
public class Pair {
public int sum;
public int nbNodes;
public Pair(int elt1, int elt2) {
this.sum = elt1;
this.nbNodes = elt2;
}
}

Resources