Search code examples
javaappletawtpaint

Applet - Simple squares and cube with for loop


I need to write an applet that calculates the squares and cubes of the numbers from 0-10 and draws the resulting values in a table format as follows:

Using a for loop. I made the application and it worked fine. I am using eclipse and when try to declare a string to be a number. I receives errors please help! I have attempted to draw the class from my package and it would not work also.

0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000

import java.applet.Applet;
import java.awt.Graphics;

public class countApplet extends Applet 
{

    public void paint(Graphics g) 
    {
        for ( int count = 0 ; count < 10 ; count++ ) 
        {
            count = num1(0); 

            g.drawString( "" , 20  , 20 * ( count + 1 ) );
            g.drawString( "" , 70  , 20 * ( count ^2 ) );
            g.drawString( "" , 120 , 20 * ( count + 1 ) );
        }
    }

}

This is my class I attempt to draw from:

import javax.swing.JOptionPane;
import java.applet.*;
import java.awt.*;

public class calculate
{

    public static void main (String args[])
    {
        int num1 = 0;

        System.out.println("Number" +"\t" +"Square" + "\t" + "Cube");   
        for(int i = 0; i <= 10; i++) 
        {

            System.out.println(i +"\t"+ i * i+"\t" + i* i * i);
        }

    }
}

Maybe a better explanation of how to use Math.sqrt in an applet would help me also?


Solution

  • Error in for loop in your countApplet class.Find the below code

    for ( int count = 0 ; count < 10 ; count++ ) 
    {
        //count = num1(0); 
        g.drawString( String.valueOf(count) , 20  , 20 * ( count + 1 ) );
        g.drawString( String.valueOf(count*count) , 70  , 20 * ( count + 1 ) );
        g.drawString( String.valueOf(count*count*count) , 120 , 20 * ( count + 1 ) );
    }