0

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • What is the error message? – Jens Feb 05 '15 at 06:29
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Feb 06 '15 at 01:09
  • @AndrewThompson yes is its due to the teacher. I cant tell my graduate teacher that ..lol ill really fail. he showed me what he wanted. – DJ Covington Feb 06 '15 at 22:38
  • *"I cant tell my graduate teacher that.."* You could always drop an anonymous note off to them. But really they ought to be ashamed of themselves for being so stuck in the past. – Andrew Thompson Feb 06 '15 at 23:09
  • lol he is of age and has a doctrine – DJ Covington Feb 07 '15 at 00:39

2 Answers2

0

Naming convention: Class names should start with a capital letter, i.e. Calculate instead of calculate and CountApplet.

Also, having a function called num1 and a variable with the same name is bad practice.

Besides, you are declaring num1 ind main() but never using it.

barq
  • 3,681
  • 4
  • 26
  • 39
0

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