May I know what is the argument need to sset in ()? 0 or 1? My datasets have three classes.
System.out.println("False Negative % = " + eval.falseNegativeRate(1));
System.out.println("True Negative % = " + eval.trueNegativeRate(1));
System.out.println("True Positive % = " + eval.truePositiveRate(1));
System.out.println("Accuracy % = " + accuracy);
System.out.println("Precision % = " + eval.precision(1));
System.out.println("Recall % = " + eval.recall(1));
System.out.println("F-Measure % = " + eval.fMeasure(1));
System.out.println("Error rate % = " + eval.errorRate());
Related
Doing code for my java class, program runs as expected the only issue I'm having is if the interstate is, for example, 405 when it prints it prints 05 marking the question wrong because the question is just looking for 5 . Any help?
import java.util.Scanner;
highwayNumber = scnr.nextInt();
if ((highwayNumber > 999) || (highwayNumber < 1)) {
System.out.println(highwayNumber + " is not a valid interstate highway number.");
}
else if ((highwayNumber < 100) && (highwayNumber > 0) && (highwayNumber % 2 == 0)) {
System.out.println("The " + highwayNumber + " is primary, going east/west.");
}
else if ((highwayNumber < 100) && (highwayNumber > 0) && (highwayNumber % 2 != 0)) {
System.out.println("The " + highwayNumber + " is primary, going north/south.");
}
else if ((highwayNumber > 99) && (highwayNumber < 1000) && (highwayNumber % 2 == 0)) {
System.out.println("The " + highwayNumber + " is auxiliary, serving the " + String.valueOf(highwayNumber).substring(1) + ", going east/west.");
}
else if ((highwayNumber > 99) && (highwayNumber < 1000) && (highwayNumber % 2 != 0)) {
System.out.println("The " + highwayNumber + " is auxiliary, serving the " + String.valueOf(highwayNumber).substring(1) + ", going north/south.");
}
}
}
Just replace substring with % modulus operator:
System.out.println("The " + highwayNumber + " is auxiliary, serving the " + String.valueOf(highwayNumber % 100) + ", going east/west.");
System.out.println("The " + highwayNumber + " is auxiliary, serving the " + String.valueOf(highwayNumber % 100) + ", going north/south.");
I am learning Java and am making code that converts pennies in to change. It is completed however, I am unsure what to enter for the while loop. I would have used while(!change.equals("END")) but this can't be done because change is an integer, so what can I do?
class Main {
public static void main(String args[]) {
System.out.print("#Please enter the amount of change : ");
int change = BIO.getInt();
if (change <= 500 && change >= 1) {
System.out.print("Amount" + "\t" + "Coins" + "\n");
}
while (change =) {
int twopounds, pounds, fifty, twenty, ten, five, two, one;
twopounds = change / 200;
int left = change % 200;
pounds = left / 100;
left = left % 100;
fifty = left / 50;
left = left % 50;
twenty = left / 20;
left = left % 20;
ten = left / 10;
left = left % 10;
five = left / 5;
left = left % 5;
two = left / 2;
left = left % 2;
one = left / 1;
int nbCoins = twopounds + pounds + fifty + twenty + ten + five + two + one;
if (change > 500 || change < 1) {
System.out.print("Invalid amount " + change + "p" + "\n");
}
if (change <= 500 && change >= 1) {
if (nbCoins == 1) {
System.out.print(change + "p " + "\t" + nbCoins + " coin ");
} else {
System.out.print(change + "p " + "\t" + nbCoins + " coins ");
}
if (twopounds > 0) {
System.out.print(twopounds > 1 ? twopounds + "*200p " : "200p ");
}
if (pounds > 0) {
System.out.print(pounds > 1 ? pounds + "*100p " : "100p ");
}
if (fifty > 0) {
System.out.print(fifty > 1 ? fifty + "*50p " : "50p ");
}
if (twenty > 0) {
System.out.print(twenty > 1 ? twenty + "*20p " : "20p ");
}
if (ten > 0) {
System.out.print(ten > 1 ? ten + "*10p " : "10p ");
}
if (five > 0) {
System.out.print(five > 1 ? five + "*5p " : "5p ");
}
if (two > 0) {
System.out.print(two > 1 ? two + "*2p " : "2p ");
}
if (one > 0) {
System.out.print(one > 1 ? one + "*1p " : "1p ");
}
System.out.print("\n");
}
System.out.print("#Please enter the amount of change : ");
change = BIO.getInt();
}
}
}
Thanks :)
It depends on what BIO is. Generally, there are options like .hasNextToken() if it is Enumerable. But, without knowing what that variable is declared as, I can't tell you what your trigger would be.
maybe this will work:
boolean flag = true;
while(flag){
//do things
if(condition_when_you_want_your_loop_to_stop){
flag = false;
}
}
I need to modify the code so that,
its structure is better
it is more readable
it uses methods and parameters
only one statement needs to be changed if a different number of integers is to be input
My new code is at the bottom but it's not working yet, can't quite figure out what else I need to do to it. If anyone could help that would be fab.
Original Code
import java.util.Scanner;
public class MakeMeBetter
{
public static void main(String[] args)
{
int[] a = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
Scanner b = new Scanner(System.in);
System.out.println("Please input 10 numbers in the range 1-19 :> ");
int c = b.nextInt();
while (c < 1 || c > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[0] = c;
int d = b.nextInt();
while (d < 1 || d > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[1] = d;
int e = b.nextInt();
while (e < 1 || e > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[2] = e;
int f = b.nextInt();
while (f < 1 || f > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[3] = f;
int g = b.nextInt();
while (g < 1 || g > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[4] = g;
int h = b.nextInt();
while (h < 1 || h > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[5] = h;
int i = b.nextInt();
while (i < 1 || i > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[6] = i;
int j = b.nextInt();
while (j < 1 || j > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[7] = j;
int k = b.nextInt();
while (k < 1 || k > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[8] = k;
int l = b.nextInt();
while (l < 1 || l > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
}
a[9] = l;
System.out.println("\nArray contents");
System.out.print((a[0] < 10 ? " " : "") + a[0] + " ");
System.out.print((a[1] < 10 ? " " : "") + a[1] + " ");
System.out.print((a[2] < 10 ? " " : "") + a[2] + " ");
System.out.print((a[3] < 10 ? " " : "") + a[3] + " ");
System.out.print((a[4] < 10 ? " " : "") + a[4] + " ");
System.out.print((a[5] < 10 ? " " : "") + a[5] + " ");
System.out.print((a[6] < 10 ? " " : "") + a[6] + " ");
System.out.print((a[7] < 10 ? " " : "") + a[7] + " ");
System.out.print((a[8] < 10 ? " " : "") + a[8] + " ");
System.out.print((a[9] < 10 ? " " : "") + a[9] + " ");
System.out.println();
boolean noSwap = false;
int startAt = 0;
int stopAt = 9;
while (startAt < stopAt && noSwap == false)
{
noSwap = true;
for (int m=startAt; m<stopAt; m++)
{
if (a[m] > a[m+1])
{
int t = a[m];
a[m] = a[m+1];
a[m+1] = t;
noSwap = false;
}
}
stopAt = stopAt - 1;
}
System.out.println("\nArray contents");
System.out.print((a[0] < 10 ? " " : "") + a[0] + " ");
System.out.print((a[1] < 10 ? " " : "") + a[1] + " ");
System.out.print((a[2] < 10 ? " " : "") + a[2] + " ");
System.out.print((a[3] < 10 ? " " : "") + a[3] + " ");
System.out.print((a[4] < 10 ? " " : "") + a[4] + " ");
System.out.print((a[5] < 10 ? " " : "") + a[5] + " ");
System.out.print((a[6] < 10 ? " " : "") + a[6] + " ");
System.out.print((a[7] < 10 ? " " : "") + a[7] + " ");
System.out.print((a[8] < 10 ? " " : "") + a[8] + " ");
System.out.print((a[9] < 10 ? " " : "") + a[9] + " ");
System.out.println();
double n = (a[0] + a[1] + a[2] + a[3] +
a[4] + a[5] + a[6] + a[7] +
a[8] + a[9]) / 10;
System.out.println("The minimum number is: " + a[0]);
System.out.println("The maximum number is: " + a[9]);
System.out.println("The average value is: " + n);
System.out.println("The median is: " + a[4]);
}
}
New Code
import java.util.Scanner;
import java.math.*;
public class MakeMeBetterImproved {
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
int[] numbers = new int[10];
int array = 0;
System.out.println("Enter 10 integers: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = kybd.nextInt();
int MyIntArray = array + i;
}
System.out.println("The minimum number is: " + math.min);
System.out.println("The maximum number is: " + math.max);
System.out.println("The average value is: " + math.average);
System.out.println("The median is: " + math.median);
}
}
Consider using a more dynamic structure than a primitive array.
I like ArrayLists: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
Reorganize your long code into while or for loops.
An example:
ArrayList<Integer> a = new ArrayList<Integer>();
Scanner b = new Scanner(System.in);
Integer c = null;
while (a.size() != 10) {
System.out.println("Please input a number in the range 1-19 :> ");
c = b.nextInt();
while (c < 1 || c > 19)
{
System.out.println("Not in the range 1-19, please try again :> ");
c = b.nextInt();
}
a.add(c);
}
System.out.println("\nArray contents");
for (int i=0; i<10; i++) {
System.out.print((a.get(i) < 10 ? " " : "") + a.get(i) + " ");
}
(And so on...)
Could you try this? I made a few changes.
package p2;
import java.util.Arrays;
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
int[] numbers = new int[10];
int sum = 0;
System.out.println("Enter 10 integers: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = kybd.nextInt();
sum += numbers[i];
}
Arrays.sort(numbers);
System.out.println("The minimum number is: " + numbers[0]);
System.out.println("The maximum number is: " + numbers[9]);
System.out.println("The average value is: " + sum / numbers.length);
System.out.println("The median is: " + median(numbers));
}
public static double median(int[] m) {
int middle = m.length / 2;
if (m.length % 2 == 1) {
return m[middle];
} else {
return (m[middle - 1] + m[middle]) / 2.0;
}
}
}
In a recent game I'm developing, I've made shops.
When you buy an item and try to sell it, the selling price drops to 75% of what the buying price is.
After buying an item for 154 gold pieces, It says the shop will buy for 115.5 gold pieces, but you get 115 gold pieces from selling.
I wish to remove the ".5" from "115.5"
Any help is appreciated.
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000 && ShopValue < 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + (ShopValue*.75 / 1) + " coins)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000000) + " billion)";
}
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+ShopAdd+"</col> coins");
}
}
I would just use Math.floor.
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000 && ShopValue < 1000000) {
ShopAdd = " (" + Math.floor(ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + Math.floor(ShopValue*.75 / 1) + " coins)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + Math.floor(ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000000000) {
ShopAdd = " (" + Math.floor(ShopValue*.75 / 1000000000) + " billion)";
}
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+ShopAdd+"</col> coins");
}
}
If you wanted to be a little more generous to your players you might use Math.ceil instead ;-)
You can get what you want by either:
Decalring ShopAdd as an integer. This might cause other problems if you're (going to be) using ShopAdd somewhere else that needs it to be a floating point data type though.
Casting ShopAdd into an int right before it's printed. This is a quick fix, and isn't that great if you plan to print ShopAdd in many places, because they'll all have to be casted.
Some example java code:
public class PrintingNumbers {
public static void main(String[] args) {
int i = 1;
double d = 1;
System.out.println("printing integer: " + i);
System.out.println("printing double: " + d + " (not what you want)");
System.out.println("printing casted double: " + (int) d);
}
}
Output of above java code:
printing integer: 1
printing double: 1.0 (not what you want)
printing casted double: 1
In your case, option 1 would look something like this:
int ShopAdd // declare ShopAdd as an integer
...
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000000) + " billion)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000) {
ShopAdd = " (" + (ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + (ShopValue*.75 / 1) + " coins)";
}
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+ShopAdd+"</col> coins");
}
}
And, option 2 would look like this:
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000000) + " billion)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000) {
ShopAdd = " (" + (ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + (ShopValue*.75 / 1) + " coins)";
}
// casting ShopAdd into an int when printing
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+(int)ShopAdd+"</col> coins");
}
}
I hope this helps! :)
As Kayaman already wrote in his comment, I would just cast it to an int or to an long - This will cut of the decimals without rounding.
If you want an integer as a result, the simple solution is to use integer maths.
long ShopValue = (long) (getItemShopValue(removeId, 1, removeSlot))*3/4;
String ShopAdd = " (";
if (ShopValue >= 750000000) {
ShopAdd = ShopValue / 750000000 + " billion";
} else if (ShopValue >= 1000000) {
ShopAdd = ShopValue / 750000 + " million";
} else if (ShopValue >= 1000) {
ShopAdd = ShopValue / 750 + "k";
} else {
ShopAdd = ShopValue + " coins";
}
ShopAdd += ")";
Note: your current implementation will print 800 as 0k
You can use Math.floor() as others said.
Apart from that: you may have a bug in your code logic. The 3rd and the 4th branches of the if statement are unreachable as the 2nd if clause covers them
if (ShopValue >= 100)
The correct way is to arrange them such that comparisons with greater numbers comes before comparisons with smaller numbers:
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000000) + " billion)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000) {
ShopAdd = " (" + (ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + (ShopValue*.75 / 1) + " coins)";
}
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+ShopAdd+"</col> coins");
}
}
I have to write a program for my Intro to CS class as a project. The program must
ask the user for a min value (can be negative)
ask the user for a max value (also may be negative, must be greater than min though)
produce twenty random numbers that are in between the min/max values given by the user and
produce an average of the twenty random numbers produced.
I thought I had completed the program but the numbers are never random when I execute the program, all twenty numbers come out to be the same value as the max value I enter into emacs. And of course the average is the same as the max value in this case as well. This is my program so far...
import java.util.Scanner;
import java.util.Random;
public class CS_ProjectOne
{
public static void main(String[] args)
{
int minimum_integer;
int maximum_integer;
int rn1;
int rn2;
int rn3;
int rn4;
int rn5;
int rn6;
int rn7;
int rn8;
int rn9;
int rn10;
int rn11;
int rn12;
int rn13;
int rn14;
int rn15;
int rn16;
int rn17;
int rn18;
int rn19;
int rn20;
double total;
double average;
Scanner keeper;
Scanner keeper2;
Random gen;
gen = new Random();
keeper = new Scanner(System.in);
System.out.print("Please enter an integer for the MINIMUM value: ");
minimum_integer = keeper.nextInt();
keeper2 = new Scanner(System.in);
System.out.print("Please enter an integer for the MAXIMUM value: ");
maximum_integer = keeper2.nextInt();
rn1 = gen.nextInt();
rn1 = rn1 % minimum_integer + maximum_integer;
rn2 = gen.nextInt();
rn2 = rn2 % minimum_integer + maximum_integer;
rn3 = gen.nextInt();
rn3 = rn3 % minimum_integer + maximum_integer;
rn4 = gen.nextInt();
rn4 = rn4 % minimum_integer + maximum_integer;
rn5 = gen.nextInt();
rn5 = rn5 % minimum_integer + maximum_integer;
rn6 = gen.nextInt();
rn6 = rn6 % minimum_integer + maximum_integer;
rn7 = gen.nextInt();
rn7 = rn7 % minimum_integer + maximum_integer;
rn8 = gen.nextInt();
rn8 = rn8 % minimum_integer + maximum_integer;
rn9 = gen.nextInt();
rn9 = rn9 % minimum_integer + maximum_integer;
rn10 = gen.nextInt();
rn10 = rn10 % minimum_integer + maximum_integer;
rn11 = gen.nextInt();
rn11 = rn11 % minimum_integer + maximum_integer;
rn12 = gen.nextInt();
rn12 = rn12 % minimum_integer + maximum_integer;
rn13 = gen.nextInt();
rn13 = rn13 % minimum_integer + maximum_integer;
rn14 = gen.nextInt();
rn14 = rn14 % minimum_integer + maximum_integer;
rn15 = gen.nextInt();
rn15 = rn15 % minimum_integer + maximum_integer;
rn16 = gen.nextInt();
rn16 = rn16 % minimum_integer + maximum_integer;
rn17 = gen.nextInt();
rn17 = rn17 % minimum_integer + maximum_integer;
rn18 = gen.nextInt();
rn18 = rn18 % minimum_integer + maximum_integer;
rn19 = gen.nextInt();
rn19 = rn19 % minimum_integer + maximum_integer;
rn20 = gen.nextInt();
rn20 = rn20 % minimum_integer + maximum_integer;
System.out.println("Your random numbers are: " + rn1 + ", " + rn2 + ", " + rn3 + ", " + rn4 + ", " + rn5 + ", " + rn6 + ", " + rn7 + ", " + rn8 + ", " + rn9 + ", " + rn10 + ", " + rn11 + ", " + rn12 + ", " + rn13 + ", " + rn14 + ", " + rn15 + ", " + rn16 + ", " + rn17 + ", " + rn18 + ", " + rn19 + ", " + rn20 + ".");
total = rn1 + rn2 + rn3 + rn4 + rn5 + rn6 + rn7 + rn8 + rn9 + rn10 + rn11 + rn12 + rn13 + rn14 + rn15 + rn16 + rn17 + rn18 + rn19 + rn20;
average = total / 20;
System.out.print("The average is " + average);
}
}
Could anyone be of some help and steer me in the right direction? I'm sure there is something I'm not doing correctly (or many things) I just don't know what exactly that is.
Here is a shorter version of me trying to get this to work...
import java.util.Random;
import java.util.Scanner;
public class p1
{
public static void main(String[] args)
{
Random gen;
gen = new Random();
Scanner keeper;
int range, user_min, user_max, num1, num2, num3, total;
double average;
keeper = new Scanner(System.in);
System.out.print("Please enter a maximum integer value: ");
user_max = keeper.nextInt();
keeper = new Scanner(System.in);
System.out.print("Please enter a minimum integer value: ");
user_min = keeper.nextInt();
range = user_max - user_min + 1;
num1 = gen.nextInt() % range + user_min;
num2 = gen.nextInt() % range + user_min;
num3 = gen.nextInt() % range + user_min;
System.out.print("The random numbers computed are " + num1 + ", " + num2 + ", " + num3 + ", ");
}
}
Why can't this handle negatives? And my values still aren't within the specified range? HELP?
Some tips/hints:
Use an array for the 20 numbers. Then when you find a mistake, you only have to correct it once rather than 20 times.
Random number generators have seeds. Make sure that you provide a different seed each time, otherwise your random number generator will always generate the same set of random numbers.
How many values can there be between MIN and MAX? For example, how many different values can there be between 3 and 7? 45 and 80? 3 and 1200? Find the pattern.
Understand why your program needs the '%' operator. What is being achieved by doing "gen.nextInt() % NUM"?
Random List Example:
(In Python instead of Java for ease of demonstration.)
1 #!/usr/bin/env python
2 def spawn(user_min, user_max, rounds):
3 import random
4 sum = 0
5 user_range = user_max - user_min + 1
6 for i in range(rounds):
7 result = int((random.random() * user_range)) + user_min
8 sum = sum + result
9 print(str(i) + ': ' + str(result))
10 mean = sum / rounds
11 print('range: ' + str(user_range))
12 print('mean: ' + str(mean))
The lines of interest for you are:
Line 5: This determines the spread of the random number.
Line 7: This fetches a random value of range [0.0, 1.0); multiplies by the desired user range; then casts to integer to 'chop off' the trailing decimal points; and then adds the result to the minimum allowed value of the range.
For example:
range = (10) - (-5) + 1 = 16
a_result = (int)(0.14159265359 * 16) + -5 = 3 - 5 = -3
another_result = (int)(0.71828182845 * 16) + -5 = 11 - 5 = 6
The line
rn1 = rn1 % minimum_integer + maximum_integer;
is wrong. Consider for example generating numbers in the range 1 to 6. Then you are calculating
rn1 = rn1 % 1 + 6;
Now, rn1 % 1 is always equal to 0, so you always get 6. You should use a different formula.
Thats what I came up with..
import java.util.*;
class RandNegToPos {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int array[]=new int[20];
System.out.println("What is the minimum? ");
int min = scan.nextInt();
System.out.println("What is the maximum? ");
int max = scan.nextInt();
if (max < min)
System.out.println("Sorry the maximum is less than the miniumum.");
else{
int rnd = (int) (min + Math.random() * ((max) - min));
for (int i=0; i < 20 ; i++)
{
rnd = (int) (min + Math.random() * ((max) - min));
array[i] = rnd;
}
int sum=0;
for (int t : array)
sum += t;
System.out.println("Your randoms are:");
System.out.println(Arrays.toString(array));
System.out.println("The average is: " + (sum/20) );
}
}
}