Creating Method That Prints The Square Root Of A Number Up To A Certain Number?
Feb 27, 2015
I am trying to create a method that prints the square root of a number up to a certain number. It needs to take a single int parameter for example "n" , and then print all of the (positive) even perfect squares less than n, each on a separate line. I want the method to be called something like this:
public void Squares(int n) {
}
I need the output to look something like this:
Example: if n = 40, your code should print
4
16
36
So I have been working for a few hours now and am really stuck.
This is what I have so far:
int count = 0;
int n = 4;
int max = n;
while(count < max) {
System.out.println(n);
n = n * n;
count++;
import java.util.Scanner; public class smallestnumber { public static void main(String args[]) { Scanner input=new Scanner(System.in); int smallest =0; int number;
[Code]...
here is the output of my code:
Enter the number 88 Enter the number 8 Enter the number 6 Enter the number 55
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
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.
One acre of land is equivalent to 43,560 square feet. Write a program that calculates the number of square feet in a tract of land with 3.5 acres. Hint: Multiply the size of the tract of land (in acres) by the number of square feet in an acre to get the number of square feet in the tract.
a) You must have a variable called nrAcres where you will store the number of acres in the track.
b) Output must look as follows:
Size of land in acres: 3.5 Size of land in square feet: 152460
c) if the value of the variable nrAcres is changed the output should change accordingly. In other words: it would be wrong to have the following statement:
System.out.println("Size of land in square feet: 152460");
previous statement will print the correct output but will not change if you change the value of nrAcres.
One acre of land is equivalent to 43,560 square feet. Write a program that calculates the number of square feet in a tract of land with 3.5 acres. Hint: Multiply the size of the tract of land (in acres) by the number of square feet in an acre to get the number of square feet in the tract.
a) You must have a variable called nrAcres where you will store the number of acres in the track. b) Output must look as follows:
Size of land in acres: 3.5 Size of land in square feet: 152460
c) if the value of the variable nrAcres is changed the output should change accordingly. In other words: it would be wrong to have the following statement:
System.out.println("Size of land in square feet: 152460");
previous statement will print the correct output but will not change if you change the value of nrAcres.
this is what i got so far
public class Land { public static void main(String[] args){ double nrAcres = 3.5; int feetInAcre = 43560; double feetInTract = nrAcres * feetInAcre; System.out.println("Size of land in acres: " + nrAcres + " Size of land in square feet: " + feetInTract); } }
i tried to compile it but it says
error: class names, Land, are only accepted if annotation is explicitly requested 1 error
I was given the assignment of creating a number to word program for my first college java programming homework.
Here is what I have created so far :
import java.util.*; public class PrintNumberInWord { // saved as "PrintNumberInWord.java" public static void main(String[] args) { int number = 5; Scanner sc = new Scanner (System.in); { System.out.println ("Enter a number"); System.out.println(" "); if (number == 1) {
[Code] .....
The first lines were made for us so we could follow a guideline, however, no matter what I type the command prompt displays 5 to me, I know thats because 5 is defined in the beginning but backspacing 5 causes the program not to work at all, how can I get this program to work properly?
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("+"); } } }
I have a beginning Java Program I have been working on that creates a number guessing program where the computer guesses a number that the user is thinking of within a certain range. I so far have the program below, but need getting rid of a few kinks/ adding features.
-First, I need to set it up so that the user will be prompted and give a range. It should run and produce something like this:
Welcome to this first-ever mind-guessing program!
Please pick a range (higher than 1 and no larger than 50): 32
You will choose a number between 1 and 32... and I will try to guess it.
With each of my guess, you will tell me whether I am too high (h or H), too low (l or L), match (m or M), or you want to quit (q or Q). My objective is to find the number using as few guesses as possible.
-Second, the game is supposed to give up and restart after failing the five, guesses, but for some reason, after it fails the fifth time, it prompts a fifth guess once again instead, then restarts after- I need to prevent this, so that it should look something like this:
My fourth guess is 17: h My guess is too high?
My fifth guess is 16: h *** I am unlucky this round. I give up.
Let's play!
My first guess is 10: etc..
import java.util.*; import java.lang.Math; public class numguessprac1 { // Declaring variables public static String input; public static int quit; public static int guess; public static int wins;
So I am currently writing my first assignment and have run into problems with my coding. The task was to have someone enter a 5 digit number and in return, I list each number on their respective lines. We also must create an error if a number other than 5 digits was entered. My problem is that when they enter a 1 or 2,3,4,6,7,8 digit number.. the error message occurs along with the rest of the messages (listing the numbers, etc). I want the program to end (or even re-ask to enter the numbers) if they incorrectly enter the data.
trying to write a program that takes a user inputted number and converts it to a binary number.
Here's what I have:
package com.java2novice.algos; import java.util.Scanner; public class Converter { static Scanner console = new Scanner(System.in); public void printBinaryFormat(int number){ int binary = console.nextInt();
In a forest, there are some bamboo trees .The length of each tree get doubled during winter and increases by one unit in summer , write a Java program to calculate the total length of n number of bamboo trees in M number of seasons. The season always starts with winter.
import java.util.Scanner; public class Tree { public static void main(String args[]) { int length; int season;
We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows.
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);
I am working on a little nothing project, but I wanted to create a random number generator for a silly game where the user guesses the number.I have used google, but they are using LOG statements, what it does.
//read the file //make the numbers 1 string line //count the number of repetitiveness in the string for the numbers //display four lowest ones
import java.io.*; import java.util.*; public class Lottery2
[Code] ....
when I run it the array gets sorted but how do i keep the data in other words
what it is supposed to do is grab numbers from a file, and give me the lowest 4 numbers back. I know the numbers using the array but how do i pull out the lowest 4 and keep the data true no matter what numbers or how many of them are in the file.
I am 4 weeks into my Intro to Java course and I am having a bit of trouble with my code. I need to make a program that will take a user inputted number, space the numbers out, then add them to a total sum. What I am having a hard time with is when I enter a negative number. I can't figure out what I need to do to have my program ignore the "-" in the string.
import java.util.*; public class Week4_Programming_Problem { static Scanner console = new Scanner(System.in); public static void main(String[] args) { int number, digit, sum=0; char digitPos;
i am trying to store a number of different value cards for a number of players. E.g there can be 2 , 4 or more players and each player can have any number of cards.
I have decided to use an arraylist for cards and tried using an array for players. But after coding and reading some information online, i realised that it is not possible to have an array of arraylist.
I'd like to know how to return a new array, I wrote in a method below the main method. I want to print the array but system.out.print doesn't work for arrays apparently. What structure i should use?