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.