i am in a first semester programming class and this probably might seem like a dumb questino to everyone here but i have been working on this for along time and cant seem to come up with anything. i am stuck on 2 programs.

the first is to ask for input for user for starting and ending radius and draw a ball that begins at starting radius and ends at ending radius i was using a circle and pause class from the cd that came with my book and my program will draw the ball with original radius but doesent do anything after that here is the code:

//Make a growing ball
//Chris Bickle

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JApplet;
import javax.swing.JOptionPane;

public class GrowingBall extends JApplet
{
	public void paint(Graphics g)
	{
		super.paint(g);
		
		final int X = 200;
		final int Y = 175;
		final Color COLOR = Color.BLUE;
		int startDiameter, endDiameter;
		
		String startInput = JOptionPane.showInputDialog(null, "Please enter the starting radius: ");
		int startRadius = Integer.parseInt(startInput);
		String endInput = JOptionPane.showInputDialog(null, "Please enter the ending radius: ");
		int endRadius = Integer.parseInt(endInput);
		
		int windowWidth = getWidth();
		int windowHeight = getHeight();
		startDiameter = startRadius * 2;
		endDiameter = endRadius * 2;
		
		Circle ball = new Circle(X-startRadius, Y-startRadius, startDiameter, COLOR);
		
		while(ball.getDiameter() < endDiameter)
		{
			ball.draw(g);
			Pause.wait(0.1);
			g.clearRect(0, 0, windowWidth, windowHeight);
			startDiameter += 5;
		}
	
		ball.draw(g);
	}
}

My second question is a program that reads a string that is a binary number, validates the input, and returns the number of 1's in the input and i am screwing up somewhere. i added print statements and i know that i am getting the right number input and my count works, but i am getting an error stringindexoutofboundextension and i dont know why.

//Chris Bickle HW #5 Problem 55

import java.util.Scanner;

public class lab5A
{
	public static void main(String [] args)
	{
		System.out.println("Lab E - Pages 367-369 - Problems 55 and 69         Chris Bickle");
		System.out.println("\nProblem 55");
		
		Scanner scan = new Scanner(System.in);
		String word = "";
		System.out.print("Please enter a combination of 0's and 1's representing a binary number: ");
		String word1 = scan.nextLine();
		
		for(int x = 0; x <= word1.length(); x++)
		{
			char n1 = word1.charAt(x);
			if((n1 == '0') || (n1 == '1'))
			word += n1; 
			else
			word = word1;
		}
			
		//DONT FORGET TO TEST STRING FOR VALIDITY!!!!!
		
		//Print out number of 1's in the string
		
		int count = 0;
		for(int x = 0; x <= word1.length(); x++)
		{
		char n = word1.charAt(x);
		if (n == '1')
			{	count++;
			}
		System.out.println("N" + n);
		System.out.println("COUNT"+ count);
		}
			
		System.out.println("The number of 1's in the number you entered is " + count);	
		
		}
	}

Any help would be greatly appreciated sorry to bother everyone with my dumb questions.

Recommended Answers

All 6 Replies

Can you edit your post to include code tags please?

[code=Java] your code here [/code]

sorry that took so long this is my first time using this site and i was trying to figure that out...ps i really do want to know what i did wrong or ways i could do this better so please any help you have would be great rather than just posting a solution..the solution is good too cuz its due tomorrow but i got along way to go in school and if im getting stuck already and just get answers its probably gonna screw me later..thanks

These should really be two separate threads, but that's alright.

Let's look at your applet first. There are a couple of problems with it. Mainly, you stuck all of your logic into the paint method. The paint method is called whenever the applet needs to be repainted (many times), and you reset all of your variables every time this happens. You need to still all of your initialization code in the init method and only do the painting in the paint method.

You're also going to run into trouble with the animation; animation in applets needs to be handled in a separate thread.

But for now, clean up your code so that initialization is in the init method and only painting is in the paint method. Then we'll tackle the animation.

Hey i know that it has been a few days i was working on calc 3 stuff. I got the program to work although i dont think i did it the way you recommended. if it is different how would you have done it? Also i am having trouble centering the circle in my applet.

//Make a growing ball
//Chris Bickle

import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JApplet;
import javax.swing.JOptionPane;

public class GrowingBall extends JApplet
{			
		public void paint(Graphics g)
		{
	
		int X = 200;
		int Y = 175;
		final Color COLOR = Color.BLUE;
		int startDiameter, endDiameter;
		
		String startInput = JOptionPane.showInputDialog(null, "Please enter the starting radius: ");
		int startRadius = Integer.parseInt(startInput);
		String endInput = JOptionPane.showInputDialog(null, "Please enter the ending radius: ");
		int endRadius = Integer.parseInt(endInput);
		if(endRadius <= startRadius)
			{
			System.out.println("Your input was not valid");
			return;
			}
		startDiameter = startRadius * 2;
		endDiameter = endRadius * 2;
		
		super.paint (g);
		
		int increment = 5;
		
		while(startDiameter <= endDiameter)
			{
				Circle ball = new Circle(X - , Y, startDiameter, COLOR);
				ball.draw(g);
				Pause.wait(0.5);
				startDiameter += increment;	
				//X -= increment * 2;
				//Y -= increment * 2;
							
			}
		
		}
}

Also i am having trouble centering the circle in my applet.

You need to use the getWidth() and getHeight() methods.

if it is different how would you have done it?

Here's a rough draft of my version:

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

public class GrowingBall extends JApplet {
    private int currentDiameter, endDiameter;

    public void init() {
        getStartingAndEndingDiameters();                
        
        new Thread(new Runnable() {
            public void run() {
                final int increment = 2;
                
                while (currentDiameter < endDiameter) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        return;
                    }

                    currentDiameter = Math.min(currentDiameter + increment, endDiameter);
                    repaint();
                }
            }
        }).start();
    }
    
    private void getStartingAndEndingDiameters() {
        while (true) {
            currentDiameter = 2 * Integer.parseInt(JOptionPane.showInputDialog(getContentPane(), "Please enter the starting radius:"));
            endDiameter = 2 * Integer.parseInt(JOptionPane.showInputDialog(getContentPane(), "Please enter the ending radius:"));
            
            if (currentDiameter < endDiameter)
                break;
            
            JOptionPane.showMessageDialog(getContentPane(), "The ending diameter must be greater than the starting diameter.");
        }
    }
    
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        g2d.setColor(Color.BLUE);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int x = (getWidth() - currentDiameter) / 2;
        int y = (getHeight() - currentDiameter) / 2;
        g2d.fillOval(x, y, currentDiameter, currentDiameter);
    }
}

Wow that is way beyond anything we have even seen so far i see what you mean about breaking everything up but we are just starting to get into that stuff and i think if i turned in anything that resembles what you have i would be thrown out of school lol thanks for all your help i managed to get it working reasonably well and really appreciated your insight

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.