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

View Replies


ADVERTISEMENT

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

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

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

Creating Java Program That Prints The Smallest Number

May 9, 2015

Here is my code :

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

[Code]...

Why is it printing 0 instead of 1?

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

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

Calculate Number Of Square Feet In A Tract Of Land With 3.5 Acres

Jan 27, 2014

1. Land Calculation

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.

View Replies View Related

Calculate Number Of Square Feet In A Tract Of Land With 3.5 Acres

Jan 27, 2014

Land Calculation

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

what is wrong with my code

View Replies View Related

Creating Number To Word Program?

Apr 1, 2015

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?

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

Number Guessing Program - Computer Guesses A Number That User Is Thinking Of Within Certain Range

Mar 28, 2015

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;

[Code] ....

View Replies View Related

User Enter 5 Digit Number And In Return List Each Number On Their Respective Lines

Sep 16, 2014

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.

View Replies View Related

Program That Takes A User Inputted Number And Converts It To A Binary Number

May 5, 2014

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();

[Code]...

Bugs on:

Line 13
Line 17
Line 23

View Replies View Related

Calculate Total Length Of N Number Of Bamboo Trees In M Number Of Seasons

Feb 20, 2014

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;

[Code] ....

View Replies View Related

Compute Recursively Total Number Of Blocks In Triangle With Given Number Of Rows

Jul 8, 2014

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.

triangle(0) → 0

triangle(1) → 1

triangle(2) → 3

View Replies View Related

Program Is To Accept Number And Display New Number After Removing All Zeros

Dec 29, 2014

ex
Sample input:2340980
Sample Output:23498

this is the program i have tried but is dosent work

import java.util.*;
class zero
{
public static void main(String args[])
{

[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

Creating Magic Square - 2D Array And Looping

Sep 12, 2014

How to create a MAGIC SQUARE, i just wanted to learn the logic of it .. with 2d array and looping..

View Replies View Related

How To Create Simple Random Number Generator For Number Between 1 -10

May 18, 2015

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.

View Replies View Related

I/O Arrays - Count Number Of Repetitiveness In String For The Number

May 19, 2014

//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.

View Replies View Related

Change Negative Number To Positive Number?

Nov 20, 2014

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;

[code]....

View Replies View Related

Store A Number Of Different Value Cards For A Number Of Players?

Feb 18, 2014

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.

View Replies View Related

Random Method - Spit Out A Number Between 0 And 1

Jan 23, 2014

I having a bit of trouble understanding how this works

Math.random();

I know its going to spit out a number between 0 and 1 but I dont know why its in that range if there are no numbers there.

The second part I'm having trouble understanding is how to adjust it for example make it between 1 and 100.

View Replies View Related

How To Return Array From A Method / Back Into Main Method That Prints Out Stuff

May 27, 2014

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?

View Replies View Related

How To Ensure That Method Won't Return Same Random Number Twice In A Row

Jul 7, 2014

I want to use this idea in a different context but for the sake of the argument lets suppose I have this method:
 
public int random number(int range)
{
Random rand = new Random();
int randomNumber = rand.nextInt(range);
.
.
.
return randomNumber;
}

How to avoid getting the same random number twice in a row?

View Replies View Related







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