Blank return value( sum of two numbers) - methods

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.

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

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

Question about LSD radix sort

I have the following code:
public class LSD{
public static int R=1<<8;
public static int bytesword=4;
public static void radixLSD(int a[],int l,int r){
int aux[]=new int[a.length];
for (int d=bytesword-1;d>=0;d--){
int i, j;
int count[]=new int[R+1];
for ( j=0;j<R;j++) count[j]=0;
for (i=l;i<=r;i++)
count[digit(a[i],d)+1]++;
for (j=1;j<R;j++)
count[j]+=count[j-1];
for (i=l;i<=r;i++)
aux[count[digit(a[i],d)]++]=a[i];
for (i=l;i<=r;i++)
a[i]=aux[i-1]; // <-- Line 19
}
}
public static void main(String[]args){
int a[]=new int[]{3,6,5,7,4,8,9};
radixLSD(a,0,a.length-1);
for (int i=0;i<a.length;i++){
System.out.println(a[i]);
}
}
public static int digit(int n,int d){
return (n>>d)&1;
}
}
At runtime it throws the following exception:
java.lang.ArrayIndexOutOfBoundsException: -1
at LSD.radixLSD(LSD.java:19)
at LSD.main(LSD.java:29)
Why is that happening?
I don't know enough about radix sort to know what your code should be, but why it's currently failing is pretty clear. You're calling radixLSD and passing 0 for l:
radixLSD(a,0,a.length-1);
When you get to this code:
for (i=l;i<=r;i++)
a[i]=aux[i-1];
In the first pass, i is set to l (0), and you try to do aux[i-1], or aux[-1], which is out of bounds

Resources