Can anyone tell me why my calculateCoin function doesnt show up? - java

Can anyone help me why the calculateCoin function doesn't show up? Basically what it does is, it calculates the coin, that was generated randomly in the drawMap function within 20% chance.
What I did and not sure doing right is, I called the calculateCoin function IN the drawMap function, and then I call the drawMap in the main.
public static void main(String[] args) {
Main main = new Main();
System.out.println(main.drawMap());
}
public int[][] drawMap(){
int[][] map = new int[5][5];
char coin = 'o';
for(int i =0; i<map.length; i++){
for(int j =0; j<map[i].length; j++){
map[i][j] = (int)(Math.random()*10);
if(map[i][j]<2){
System.out.print(coin+ " ");
}
else
System.out.print("*"+ " ");
}
System.out.println("");
}
calculateCoin(map, coin);
System.out.println("");
return map;
}
public int calculateCoin(int[][] map, char coin){
int result = 0;
for(int i = 0; i<map.length; i++){
for(int j = 0; j<map[i].length; j++){
if(map[i][j] == coin){
result++;
}
}
}
return result;
}

The function is actually being called but the value that you return from it is not stored in any variable. If you want something to happen after printing the map, store the result of the call in a variable and then print it.
int calculatedCoin = calculateCoin(map, coin);
System.out.println("Calculated coin: " + calculatedCoin)

try this , but basically you declare a type of a class not a method.
public class dave {
public static void main(String[] args) {
dave main = new dave();
System.out.println(dave.drawMap());
}
public static int[][] drawMap() {
int[][] map = new int[5][5];
char coin = 'o';
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
map[i][j] = (int) (Math.random() * 10);
if (map[i][j] < 2) {
System.out.print(coin + " ");
} else
System.out.print("*" + " ");
}
System.out.println("");
}
calculateCoin(map, coin);
System.out.println("");
return map;
}
public static int calculateCoin(int[][] map, char coin) {
int result = 0;
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
if (map[i][j] == coin) {
result++;
}
}
}
return result;
}
}

Related

printing a symmetrical 2d array

I want to randomly generate a symmetrical 10x10 table with "*" symbols but It all basically prints in two long rows. I know I'm doing something wrong but I'm looking at it too hard and cant see the issue.
public class Main {
public static void main(String[]args) {
int n = 10;
char[][] array = new char[n][n];
Random rd = new Random();
for(int i = 0; i < n; i++) {
for(int j = 0; j < i; j++) {
char value;
if (Math.random()> .5) {
value = '*';
}
else {
value = ' ';
}
array[i][j] = value;
array[j][i] = value;
System.out.print(array[i][j]);
System.out.println();
System.out.print(array[j][i]);
}
}
}
}```
I don't see your context here. But you can try this
public class Main {
public static void main(String[] args) {
int n = 10;
char[][] array = new char[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
char value;
if (Math.random() > .5) {
value = '*';
} else {
value = ' ';
}
array[i][j] = value;
System.out.print(array[i][j]);
}
System.out.println();
}
}
}
you're not printing row by row, also missing the diagonal element. Fixing those and some other minor changes
int n = 10;
char[][] matrix = new char[n][n];
Random rd = new Random();
for(int i = 0; i < n; i++) {
for(int j = 0; j <= i; j++) {
char value = rd.nextBoolean() ? '*':' ';
matrix[i][j] = value;
matrix[j][i] = value;
}
}
for(char[] row : matrix) {
System.out.println(row);
}

Java Insertion Sort doesnt sort

I have written a java sorting algorithm by insertion, the code compiles but doesnt sort:(. If anyone could point out any flaws, Id be very thankful, Java doesnt...
public class Sort {
public static int[] sort(int[] x) {
int[] y = new int[x.length];
for(int i = 0; i < x.length; ++i) {
int j = 0;
while (y[j] < x[i] && j < i) ++j;
for (int k = i-1; k >= j; --k) y[k+1] = y[k];
y[j]=x[i];
}
return y;
}
public static void main(String[] args) {
int[] size = new int[10];
for(int k=0; k<size.length; ++k ) {
size[k]=(int)(Math.random()*20);
System.out.println(size[k]);
}
System.out.println(sort(size));
}}
[I#39ed3c8d is returned by invoking toString() on an int array, which you then print with System.out.println.
You presumably want System.out.println(Arrays.toString(sort(size))); instead.
You are printing the reference of size i.e., [I#39ed3c8d .
This should help you to understand:
public class Sort {
public static int[] sort(int[] x) {
int[] y = new int[x.length];
for(int i = 0; i < x.length; ++i) {
int j = 0;
while (y[j] < x[i] && j < i) ++j;
for (int k = i-1; k >= j; --k) y[k+1] = y[k];
y[j]=x[i];
}
return y;
}
public static void main(String[] args) {
int[] size = new int[10];
System.out.println("Befor sorting");
for(int k=0; k<size.length; ++k ) {
size[k]=(int)(Math.random()*20);
System.out.print(size[k]);
System.out.print(" ");
}
size = sort(size);
System.out.println("\nAfter sorting");
for(int i = 0; i<size.length; i++){
System.out.print(size[i]);
System.out.print(" ");
}
}}
That random is result of:
System.out.println(sort(size));
Do this instead:
size = sort(size);
for(int k=0; k<size.length; ++k ) {
System.out.print(size[k] + " " );
}

How to get Number pattern in specific shape (java)?

I want to print number pattern like this..
but, I failed to get this triangle shape,and I am confused how to put spaces to get this shape -->
1
212
32123
4321234
Here's the code i tried so far
public class Ch {
public static void main(String[] args) {
int r =Integer.parseInt(args[0]);
for(int u=1;u<=r;u++)
{
for(int i=u;i>=1;i--)
{
System.out.print(i);
}
for(int i=2;i<=u;i++)
{
System.out.print(i);
}
System.out.println();
}
}
}
output of this code looks-like this
1
212
32123
4321234
Thanks
Just add one more step in you main loop before the other two:
for (int i = u; i < r; i++)
{
System.out.print(" ");
}
This will print spaces to make up for the "missing" numbers.
In regard to Mateusz' comment, look at this answer on how to pad your numbers with spaces to make them equally wide in case you go beyond 9:
static int padding;
public static void main(String[] args)
{
int r = Integer.parseInt(args[0]);
padding = Math.max(1, (int) Math.ceil(Math.log10(r)));
for (int u = 1; u <= r; u++)
{
for (int i = u; i < r; i++)
{
print(" ");
}
for (int i = u; i >= 1; i--)
{
print(i);
}
for (int i = 2; i <= u; i++)
{
print(i);
}
System.out.println();
}
}
private static void print(Object text)
{
System.out.print(String.format("%1$" + padding + "s", text));
}
using 2 for loops
package com.stackoverflow;
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the pyramid limit value: ");
int limit = in.nextInt();
in.close();
for (int i = 0; i < limit; i++) {
for (int j = 0; j < limit + i; j++) {
if (j < limit -i-1)
System.out.print(" ");
else{
int temp = (limit-j > 0) ? limit-j : j-limit+2;
System.out.print(temp);
}
}
System.out.println();
}
}
}
using 4 for loops
package com.stackoverflow;
import java.util.Scanner;
public class Pyramid {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the pyramid limit value: ");
int limit = in.nextInt();
in.close();
for (int i = 0; i < limit; i++) {
for(int j=0; j<limit-i; j++){
System.out.print(" ");
}
for(int j=0; j<=i; j++){
System.out.print(i-j+1);
}
for(int j=i; j>0; j--){
System.out.print(i-j+2);
}
System.out.println();
}
}
}

Creating a double mirrored triangle

I need help making a mirrored triangle like this:
* *
** **
*** ***
********
I can get each one seperatly, but I can't combine them.
public static void main(String[] args){
for( int i = 1; i <= 5; i++ ){
for( int j = 0; j < i; j++ ){
System.out.print("*");
}
System.out.println();
}
for(int i = 0; i < 6; i++)
{
for(int j = 5; j > 0; j--)
{
if(i < j)
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
You need to track the position from both sides to be able to show * at correct location. Here is the solution:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 10; j++) {
if (j <= i || j > 10 - i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
You can do it using this code.
public class Test {
public void sizeOfTri(int triSize) { //Number of lines
int line = 1;
for(int i = 0; i < triSize; i++) { //Handles Each line
int temp = triSize * 2;
for(int j = 1; j < temp; j++) { //Handles Each space on the line
if(j <= line && j == triSize || j >= temp - line && j == triSize) { //For the very last line because it needs an extra *
System.out.print("**");
} else if(j <= line || j >= temp - line) { //For normal lines
System.out.print("*");
} else if(j == triSize) {
System.out.print(" "); //For the second to last line because it needs an extra space to make it look mirrored
} else { //For normal lines
System.out.print(" ");
}
}
System.out.println();
line++;
}
}
public static void main(String[] args) {
new Test().sizeOfTri(4);
}
}
I commented on next to if statements on which part does what. This will produce an output which looks like the below when run
* *
** **
*** ***
********
Although, all the above implementations are really good ones. I thought of doing it bit differently and make use of 2-D arrays.
package algorithms;
public class DrawMirror {
static void initialize(String[][] array){
for (int i=0; i<MAX_X; i++){
for (int j=0; j < MAX_Y; j++){
array[i][j] = " ";
}
}
}
static void draw(String[][] array, int x, int y){
for (int i=0; i < y; i++){
array[x][i] = "*";
array[x][MAX_Y-i-1] = "*";
}
}
final static int MAX_X = 4;
final static int MAX_Y = 8;
public static void main(String[] args) {
String[][] array = new String[MAX_X][MAX_Y];
initialize(array);
for (int i=0; i < MAX_X ; i++){
draw(array,i,i+1);
}
for( int i = 0; i < MAX_X; i++ ){
for( int j = 0; j < MAX_Y; j++ ){
System.out.print(array[i][j]);
}
System.out.println();
}
}
}
The following code is a function with variable height.
public static void printDoubleTriangle(int height) {
for(int i = 0; i < height; i++) {
for(int j = 0; j < 2*height; j++) {
if(j <= i || (2*height - j - 1) <= i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}

How to change an array of ints to an array of strings?

Ok, the title might be deceiving. All i want to do is take my Bingo program and when the second bingo card is printed, i want to replace all the "0"'s with "X"'s. I was thinking i would have to go and change the array to an string, but i'm not surer where to start.
Here is the Bingo program:
import java.util.*;
import java.io.*;
import java.util.Arrays;
public class Bingo
{
public static final int ROWS = 5;
public static final int COLS = 5;
public static final int VERTICAL = 1;
public static final int DIAGONAL = 2;
public static final int HORIZONTAL = 3;
public static int winFound;
public static int currPick = 0;
public static int randomPick = 0;
public static int WinFound;
public static void main(String[] args)
{
int Totcards;
int[][] card = new int[ROWS][COLS];
int[] picks = new int[25];
fillCard (card);
printCard(card);
playGame(card);
printCard(card);
finalCard(card);
}
private static void fillCard (int[][] card)
{
// FileReader fileIn = new FileReader("Bingo.in");
// Bufferreader in = new Bufferreader(fileIn);
try {
Scanner scan = new Scanner(new File("bingo.in"));
for (int i=0; i<card.length; i++){
for (int j=0; j<card[0].length; j++){
card[i][j] = scan.nextInt();
}
}
} catch(FileNotFoundException fnfe) {
System.out.println(fnfe.getMessage());
}
}
private static void printCard (int[][] card)
{
System.out.println("\n\tYOUR BINGO CARD : ");
System.out.println("\n\tB I N G O");
System.out.println("\t----------------------");
for (int i=0; i<card.length; i++){
for (int j=0; j<card[0].length; j++){
System.out.print("\t" + card[i][j]);
}
System.out.print("\n");
}
}
private static void playGame (int[][] card)
{
int numPicks = 0;
System.out.println("\n\tBINGO NUMBERS PICKED AT RANDOM FROM BIN: ");
while (true)
{
markCard (card); // Generate a random num & zero-it out
winFound = checkForWin(card); // Look for zero sums
numPicks++;
if (winFound != 0)
{
if (winFound == 1)
{
System.out.print("\n\n\tYOU WIN WITH A VERTICAL WIN AFTER " + numPicks + " PICKS\n");
}
else if (winFound == 2){
System.out.print("\n\n\tYOU WIN WITH A DIAGONAL WIN AFTER " + numPicks + " PICKS\n");
}
else if (winFound == 3){
System.out.print("\n\n\tYOU WIN WITH A HORIZONTAL WIN AFTER " + numPicks + " PICKS\n");
}
announceWin (numPicks);
return;
}
}
}
private static void markCard (int[][] card)
{
int randomPick = (int) (Math.random() * 74) + 1;
for (int j = 0; j < ROWS; j++){
for (int k = 0; k < COLS; k++){
if (card[j][k]==randomPick)
card[j][k] = 0;}
}
System.out.print("\t " + randomPick + " ");
System.out.print("");
}
private static int checkForWin(int[][] card)
{
int sum=0;
for (int i = 0; i < ROWS; i++)
{
sum = 0;
for (int j = 0; j < COLS; j++)
sum += card[i][j];
if (sum == 0)
return HORIZONTAL;
}
for (int j = 0; j < COLS; j++)
{
sum = 0;
for (int i = 0; i < ROWS; i++)
sum += card[i][j];
if (sum == 0)
return VERTICAL;
}
sum = 0;
for (int i = 0; i < ROWS; i++)
sum += card[i][ROWS-i-1];
if (sum == 0)
return DIAGONAL;
sum = 0;
for (int i = 0; i < ROWS; i++)
sum += card[i][i];
if (sum == 0)
return DIAGONAL;
return WinFound;
}
private static void makeCard(int[][] card, int[] picks)
{
int count = 100;
int currPick = 0;
for (int i=0; i<count; i++){
currPick = (int)(Math.random() * 74) + 1;
System.out.print(" " + currPick + "\n");
picks[i] = currPick;
}
}
private static void announceWin(int numPicks)
{
}
private static boolean duplicate (int currPick, int[] picks, int numPicks)
{
for (int i = 0; i < numPicks; i++){
if (picks[i] == currPick){
return true;}
}
return false;
}
private static void finalCard (int[][] card)
{
Arrays.sort(card);
final String stringRep = Arrays.toString(card);
final String[] out =
stringRep.substring(1, stringRep.length() - 1).split("\\s*,\\s*");
System.out.println(Arrays.toString(out));
}
}
Try this:
System.out.print("\t" + (card[i][j] == 0 ? "X" : card[i][j]))
Why don't you just use a String[][] for the fields from the beginning? You can still compare String values with ints (with Integer.valueOf for instance) and this way you don't have to switch types runtime...

Categories

Resources