Draw Square With Asterisks (for Loops)

Apr 21, 2015

So currently I'm trying to learn how to draw a square with asterisks based on the input I give for it's sideLength.

So if I have a Square(5) it should draw a 5x5 looking like this:

*****
*****
*****
*****
*****

but then in the main method, it should draw multiple squares of given sizes, so say 5, 6, and 7. Making a square for each of the given numbers. This is what I currently have so far ....

class Square {
int sideLength; Square( int size ) {
sideLength= size;
} int getArea() {
return sideLength * sideLength;

[Code] ....

View Replies


ADVERTISEMENT

Computing Square Root Of A Number Without Using Loops?

Sep 2, 2014

Some algorithm in computing the square root of a number without using any loops?

View Replies View Related

Draw Ascending Stairs Using For Loops

Sep 27, 2014

I'm not looking for answers, well I am.. but not just straight given to me. I'm trying to learn but for this program I'm not even sure where to start. One step looks like this:

O *******
/| * *
/ * *
************

It must increase by a constant named STEPS. They ascend from left and go up right.

I'm just gonna start asking some questions I guess. Before I do anything I wanted to know whether I should have the stick man on the step drawn out with print and println statements or try to have him drawn with a loop as well? I'm not sure if my teacher wants everything drawn by characters with loops for each character, or if I can do print statements and draw the stick figure plus some stars and have a loop for multiple characters at once.

View Replies View Related

Creating Cursor For A Game That Moves Square By Square - Removing Trailing Images

Aug 31, 2014

I'm trying to create a cursor for a game that moves square by square. While it will move to the next square, though, it leaves the image of the previous cursor on the last square it was on.

As a visual explanation, this is what the program looks like on launch:

This is what it's suppose to look like after you press the right arrow key once (made by forcibly changing launch coordinates):

And this is what it actually looks like after you press the right arrow key once:

Here is the code for the program:

package cursortest;
import javax.swing.*;
import java.awt.*;
import javax.imageio.*;
import java.io.*;
import java.awt.event.*;
public class CursorTest extends JPanel implements KeyListener{

[Code] ......

I'm fully aware that I could just use g.clearRect on the area and remove it for sure, but I know for a fact I shouldn't have to as I have another program I made a long time ago that tried to do something similar without needing to resort to that.

View Replies View Related

How To Compare Square Of The Square Root Of Any Number

Feb 20, 2014

How would I compare the square of the square root of any number. The difference in these values is due to the rounding error in Java.

Example: Enter number 2

The answer I should get is: 2.000000000000004
Round off error: -4.440892

View Replies View Related

Getting Rid Of Asterisks

Mar 19, 2014

This is my code so far which is correct my only problem is that I have to make it so there doesn't become more than 50 asterisks on a line and I don't know how to do that I know the code is something divided by 50 but I dont understand where I would put it.

so instead of this

************************************************** ******************(pretend it was 100 asterisks)
it would be
****************************************(x<50 asterisks or 42)

import java.util.*;
public class Graph {
private int [] counter;
public Graph(int x) {
counter=new int[5];

[Code] ....

View Replies View Related

Bar Chart With Asterisks

Oct 12, 2014

I need to make a loop that prints out a certain number of asterisks, depending on the random number generated (includes 0-24, not including 25). I'm just in basic java programming and am stuck on this task.

These are the directions given to me:

Write a loop to do the following tasks 10 times:
-Generate a random number in the range of 0 to 24 and store in an int variable named: score
-Write loop to print a bar of asterisks proportional to the value in score :

For example: , if value stored in score is 8, the bar chart should like as follows: (value of score spaced by a tab ( ) and a series of 8 asterisks in one line)
8 ********
//end of the loop

I've tried looking for other questions like this, but this one is a random number, with no input. Yes, I know there are easier ways, but my professor is extremely picky with everything.

This is what I have so far:

public class PA5LoopsA {
public static void main(String [] args) {
//Declare variables
int score;
//Generate random number between 0 and 24
score = (int) (Math.random() * 25);

[Code] ....

Clearly, none of my formatting went through...yes, I know how to space everything out!

View Replies View Related

Bar Chart With Asterisks?

Oct 19, 2014

Here is the assignment instructions:

Overview: Use the Eclipse Java environment to create and test a program using methods that will display a bar graph.

Specifics: This assignment is an extension of the Module 5 activity, the first bar chart (asterisk) display program. You should also make use of the Bar Chart printing program of Chapter 6 (Fig. 6 in the textbook, p. 217).

Your program should use methods to accept input from the keyboard, store it in an array, and use that data to display a bar graph.

Pseudocode for the program might look like the following.

Main
declare an array of integers
get the number of asterisks in each bar
print the bar graph
get the number of asterisks in each bar
declare a Scanner input object
declare an array to fill and return

[code]....

Problem is I cannot get it to display the number of asterisks I enter as input.

View Replies View Related

How To Convert Integer To That Many Asterisks

Jan 22, 2015

int a = 5;

//how do I convert a to 5 asterisks '*'?

When I do System.out.println(a); I want there to be 5 asterisks '*****'

View Replies View Related

Displacing A Shape Made Of Asterisks

May 5, 2014

I'm making a program that prints arrows of varying tail length, head width, and displacement.The shape algorithms are working properly, but the displacement isn't working. The first arrow has zero displacement, and the second one is supposed to be bigger and start the print at 10 spaces right and 5 spaces down from the default, but it's not working. I tried changing the displacement settings of the first arrow, and no changes happened, so I know that the positions aren't registering properly in general. How to pass the xAdj and yAdj (displacement) values? I think I have the empty space formula set up, but it's still not running.

Driver:
public class Driver
{
//default values
public static final int Arrow_Default_xAdj = 0;
public static final int Arrow_Default_yAdj = 0;
public static final int Arrow_Default_tail = 5;
public static final int Arrow_Default_width = 5;

[code]...

View Replies View Related

Printing Asterisks Shape In Java

Mar 17, 2014

I am supposed to Write a program that prints a shape similar to the following shape. Maximize use of loops and minimize use of print statements.

.....*
....**
...***
..****
...***
....**
.....*

(without the "...")(i couldn't get it to stay in shape)

This is what i got so far:

Java Code: package assignment7;

public class Exercise3
{
public static void main (String[] args)
{
for (int count =0; count < 4; count++)
{
for (int j=0; j < count+1; j++)
[Code] ....

Tthis compiles to:

*
**
***
****
**
*

How do i center it to create the shape?

View Replies View Related

Creating Shape - How To Deal With Blanks And Asterisks

Mar 16, 2014

I need to create this shape:

*" "*" "*
" "*" "*" "
" "" "*" "" "

using loops. How to deal with the blank and asterisks? Its just don't work correctly for me...

View Replies View Related

Magic Square Says No

Nov 25, 2014

I'm having a small issue with my output on my code. here is what my output is: The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.

Please enter the 4 values for row 0, separated by spaces: 1 2 15 16
Please enter the 4 values for row 1, separated by spaces: 13 14 3 4
Please enter the 4 values for row 2, separated by spaces: 12 7 10 5
Please enter the 4 values for row 3, separated by spaces: 8 11 6 9
Checking square for problems:
DIAG: VALID
ROWS: VALID
COLS: VALID
RANG: VALID
MAGIC: No

MAGIC should be YES. However i keep getting it saying no.This is the correct output..The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.

Please enter the 4 values for row 0, separated by spaces: 1 2 15 16
Please enter the 4 values for row 1, separated by spaces: 13 14 3 4
Please enter the 4 values for row 2, separated by spaces: 12 7 10 5
Please enter the 4 values for row 3, separated by spaces: 8 11 6 9

Checking square for problems:
DIAG: VALID
ROWS: VALID
COLS: VALID
RANG: VALID
MAGIC: YES

Here is my code..

import java.util.Scanner;
public class pdonahue_Magic {
public static void main(String args[]) {
int[][] theSquare = new int[4][4];
Scanner s = new Scanner(System.in);
System.out.println.

("The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.");

System.out.print("Please enter the 4 values for row 0, separated by spaces: ");
theSquare[0][0] = s.nextInt();
theSquare[1][0] = s.nextInt();
theSquare[2][0] = s.nextInt();
theSquare[3][0] = s.nextInt();

[code]....

View Replies View Related

How To Make Square Which Is Filled With (dot)

Oct 17, 2014

Do I want to make something like this:

*****
*.....*
*.....*
*.....*
*****

I am stuck, I have made a code that creates this and don't know how to complete it.

*****
* *
* *
* *
*****

Code:

class Main {
public static void main( String args[] ) {
System.out.print("#Enter number of stars :");
int stars = BIO.getInt();

[Code] ....

View Replies View Related

How To Find If BigInteger Is Square

Jul 3, 2014

I have a program i m not sure how to implement :

(Square numbers) Find the first ten square numbers that are greater than Long.MAX_VALUE . A square number is a number in the form of n 2 . For example, 4, 9, and 16 are square numbers. Find an efficient approach to run your program fast.

I found two ways of solving this but i think both are way inefficient :

-A square number can be divided in lesser square numbers :

what's the square of 36 ? 36 is 2 * 3 * 2 * 3 => 4 * 9 => square is 2 * 3

-second option is to estimate a number and increase it or decrease it based on how close that number * number is to the BigInteger starting number , as as it gets closer the delta gets smaller until it gets to 1

View Replies View Related

Nth Square Root Of Any Number

May 14, 2009

This is a mathematical problem. I got one of the equations I need to compute down to

n^(1/6)

Or the 6th root of n.

Now the Java code I have for this is as follows, since Java has no nth root function I'm raising it to a power of a 1/6.

System.out.println(Math.pow(64.0, 1/6));

Now, the only thing it will print is 1.0

However the 6th root of 64 is 2!!!!

View Replies View Related

Create Square Matrix That Has Min And Max Value

May 10, 2015

I have to create a square matrix that has a min and max value as well as a size value which is given a integer value in the main method. The matrix has to be filled with random values. Also I have to add that matrix to another one in an addMatrix method and I have to subtract both in a subMatrix method. These are the requirements:

Methods:
Constructor() - receives the row and col size for myMatrix and a max and min values for range of random fill values for the matrix.
RandFill() - fills matrices with random numbers
addMatrix() - receives a matrix object. adds its myMatrix with the received object's myMatrix. The result is placed in this object's myResultMatrix.
subMatrix() - subtracts both matrices

I typed up this code but I'm not sure about some parts of it and I would creating min and max values in the Random method and in the main printing the separate 2 matrices and adding and subtracting them:

import java.util.*;
public class SquareMatrix2 {
public int size;
public int myMatrix [][];
public int myResultMatrix;
public int [][] ResultStatus;

[Code] ....

View Replies View Related

Timer - Why Does Square Not Moving

Sep 7, 2014

Why does my program write that i have problem in Timer?

public class Ball extends JPanel implements ActionListener {
Timer timer = new Timer (3, this);
int x = 0, y = 0, aX = 2, aY = 2;
public void PaintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;

[Code] ....

View Replies View Related

Creating Solid Square

May 3, 2014

I aim to create a solid square using this:

public class asteriskSquare {
public static void main(String[] args) {
for (int x = 1; x <= 4; x++){
for (int y = 1; y <= 4; y++){
System.out.println("+");
}
System.out.println("+");
}
}
}

All I'm getting is a single column of "+".

View Replies View Related

Multidimensional Array With Size Only In The First Square

Jan 14, 2014

I come to the point: I just started to learn java through various manuals and in one of them I came across a declaration of an array that I do not understand:

int[][] multiArr = new int[2][];

the manual says that you can allocate the multidimensional array multiArr by defining size in only the first square bracket but I can't undestand how you can use this array. Seems to be no way to store data with it!

View Replies View Related

Magic Square Won't Pass The Tests

Feb 11, 2015

public class MagicSquare {
public boolean isSquare(int[][] arr) {
if(arr.length == arr[0].length)
return true;
else
return false;

[Code] ....

View Replies View Related

Diamond In A Square Grid Of Dots

Nov 14, 2014

I am new to java and i am trying to make a Java application which prints a diamond in a square grid of dots whose side length is input to the application.When you run the code is should be like this:

..*..
.*.*.
*...*
.*.*.
..*..

My java code print this:

..*..
.***.
*****
.***.
..*..

Here is my code:

class Main {
public static void main(String args[]) {
System.out.println("#Enter size of Diamond :");
int longestRow = BIO.getInt();
for(int row=1 ; row<=longestRow ; ++row)

[Code] .....

View Replies View Related

Why Program Not Printing Square And Triangle

Nov 21, 2014

import java.util.Scanner;
public class justin10a

public static void main(String [] args)
{
int n;
n = getSize();
 
[Code] .....

View Replies View Related

Utility Functions For Square Matrices And Arrays

Nov 7, 2014

I am working on a number of utility functions for square matrices and arrays, and I keep having trouble with segmentation faults.

arrayUtils~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
public class ArrayUtils {
//This function takes an array a, and returns the sum between indices
//i and j, where i is the lower index and j is the upper index. int size makes
//sure that the function doesn't go out of bounds.
public static int subSum(int[] a,int i, int j) {
int sum=0;

[Code] .....

View Replies View Related

Creating A Matrix For Magic Square Project

Jan 12, 2015

I am making a program that reads a set of numbers (e.g.: 8 1 6 3 5 7 9 4 2) and prints them into a matrix.For example:

8 1 6 3 5 7 9 4 2, will produce:
8 1 6
3 5 7
9 4 2

When taking in these numbers from the user, I am not allowed to let the user state what size the matrix is (e.g.;3 x 3). Instead the program needs to determine that itself. I have written the code below to count the number of numbers inputted, but now I am stuck as to how to get them into the array. I have what is suppose to be my array written, but it is not function yet.

import java.util.*;
public class MagicSquare {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

[code]....

View Replies View Related

Word Counter - Show Number In Square Box

Jan 3, 2015

My code compiles and runs just fine, i'd just like to get creating a small square box that shows number of words used next to the "word count = ". i'd wanto to press the count words button to count and show the number in the square box. here is my code.

import javax.swing.*;
import javax.swing.text.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class wordCount extends JFrame implements ActionListener

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved