use an int Variable for range to get random number - random

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;

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

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

Modify methods so they are given an array index value of the moon whose name or radius is required

So the part of my question is ''Modify getMoonName() and getMoonRadius() so they are given an array index value of the moon whose name or radius is required.''
I've tried adding moons[i].getRadius but then end up getting ''The variable i does not exist''. Here's the code.
PLANET CLASS
public class Planet
{
private float angle=0.01;
// add class member variables here
private String name;
private float radius;
private float distance;
private float speed;
private Moon[] moons;
// add constructor here
public Planet(String n, float r, float d, float s, Moon[] m)
{
this.name=n;
this.radius=r;
this.distance=d;
this.speed=s;
this.moons=m;
}
// add other methods here
public String getName()
{
return name;
}
public float getRadius()
{
return radius;
}
public float getDistance()
{
return distance;
}
public float getSpeed()
{
return speed;
}
public Moon[] getMoons()
{
return moons;
}
public void setRadius(float r)
{
this.radius=r;
}
public String getMoonName()
{
return moons[i].getName();
}
public float getMoonRadius()
{
return moons[i].getRadius();
}
public String toString()
{
int n=0;
for (int i=0; i<moons.length; i++)
{
n++;
}
return "Planet" + name + ("Radius: " +radius +"Distance: " +distance) +n +"moons.";
}
public void printMoons()
{
for (int i=0; i<moons.length; i++)
{
System.out.println(moons[i]);
}
}
// This will display the moon when other code is completed. You don't need to understand this code.
public void display()
{
angle=angle+(0.01*speed);
pushMatrix();
rotate(angle);
translate(distance,0);
fill(255, 255, 255);
ellipse(0, 0, radius*2, radius*2);
for(Moon moon: getMoons())
moon.display();
popMatrix();
}
}`
MOON CLASS
public class Moon
{
private float angle=0.01;
// add class member variables here
private String name;
private float radius;
private float distance;
private float speed;
private int orbitalPeriod;
// add constructor here
public Moon(String n, float r, float d, float s, int o)
{
this.name=n;
this.radius=r;
this.distance=d;
this.speed=s;
this.orbitalPeriod=o;
}
// add other methods here
public String getName()
{
return name;
}
public float getRadius()
{
return radius;
}
public float getDistance()
{
return distance;
}
public float getSpeed()
{
return speed;
}
public float getOrbitalPeriod()
{
return orbitalPeriod;
}
public void setName(String n)
{
this.name=n;
}
public void setOrbitalPeriod(int o)
{
this.orbitalPeriod=o;
}
public String toString()
{
return ("Moon : " +name +" "+"orbit="+orbitalPeriod);
}
// This will display the moon when other code is completed. You don't need to understand this code.
public void display()
{
angle=angle+(0.01*speed);
pushMatrix();
rotate(angle);
translate(distance, 0);
fill(149, 149, 149);
ellipse(0, 0, radius*2, radius*2);
popMatrix();
}
}
Let's look at this function:
public String getMoonName()
{
return moons[i].getName();
}
Where do you think the i variable is defined? Your instructions say to take an argument, but this function does not take any arguments.
As a small example, let's say I had this function:
public void printMessage(){
println("Hello!");
}
If I wanted to modify that function to take a parameter, I would have to add that to the method like this:
public void printMessage(String message){
println(message);
}
You have to do something similar with your getMoonName() function.
If you're still stuck, please post a small example like mine instead of your whole sketch, and we'll go from there.

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