Magic 8 Ball Code

Dec 5, 2014

I am writing a three part program that essentially works like a magic 8 ball. I have the switch/case part which gives the answers from a random number generator, then I also have a graphics file to create the ball as a visual. Lastly I have the frame to display the ball. The problem I am having is that, I am trying to make the program so that when you type in a question and press return the ball will show the answer and then you can type another question and press return and the ball will show a different answer. To quit you would simply type "quit". Right now however the ball is not showing up in my frame until I type quit, then to ask another question I have to end the run and start over again. Here is my program so far:

Case/Switch:
public class MagicEightBall
{
private int number;
private String answer;

[code]....

View Replies


ADVERTISEMENT

Ball Object Which Implements Runnable Interface And Traces Various Positions Of A Ball

Feb 11, 2014

a) I have a Ball Object which implements the Runnable interface and traces the various positions of a ball.

b) I then have a Ball_Bounce JPanel inside a JFrame which creates two instances of the Ball object and then paints them to the JPanel.

As per my understanding, when the main() program in Ball_Bounce.java is started, there a total of three threads running in this program, one for each ball and one for the main(). What I cannot understand is whenever the balls collide, I end up getting the "Collision" message twice even though the collision is checked only in the main() thread.

[#]public class Ball implements Runnable
{
private boolean xUp, yUp, xUp1, yUp1;
private int x, y, xDx, yDy;
private final int MAX_X = 500, MAX_Y = 500;
private boolean flag = true;
private static Thread ball;
 
[code]...

View Replies View Related

Magic Square Says No

Nov 25, 2014

I'm having a small issue with my output on my code. here is what my output is: The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.

Please enter the 4 values for row 0, separated by spaces: 1 2 15 16
Please enter the 4 values for row 1, separated by spaces: 13 14 3 4
Please enter the 4 values for row 2, separated by spaces: 12 7 10 5
Please enter the 4 values for row 3, separated by spaces: 8 11 6 9
Checking square for problems:
DIAG: VALID
ROWS: VALID
COLS: VALID
RANG: VALID
MAGIC: No

MAGIC should be YES. However i keep getting it saying no.This is the correct output..The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.

Please enter the 4 values for row 0, separated by spaces: 1 2 15 16
Please enter the 4 values for row 1, separated by spaces: 13 14 3 4
Please enter the 4 values for row 2, separated by spaces: 12 7 10 5
Please enter the 4 values for row 3, separated by spaces: 8 11 6 9

Checking square for problems:
DIAG: VALID
ROWS: VALID
COLS: VALID
RANG: VALID
MAGIC: YES

Here is my code..

import java.util.Scanner;
public class pdonahue_Magic {
public static void main(String args[]) {
int[][] theSquare = new int[4][4];
Scanner s = new Scanner(System.in);
System.out.println.

("The magic value for your square is 34, which means that every row, column and diagonal of your square must add up to that number.");

System.out.print("Please enter the 4 values for row 0, separated by spaces: ");
theSquare[0][0] = s.nextInt();
theSquare[1][0] = s.nextInt();
theSquare[2][0] = s.nextInt();
theSquare[3][0] = s.nextInt();

[code]....

View Replies View Related

Magic Square Won't Pass The Tests

Feb 11, 2015

public class MagicSquare {
public boolean isSquare(int[][] arr) {
if(arr.length == arr[0].length)
return true;
else
return false;

[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

Magic Square Program - File Not Found Exception Error

Apr 25, 2014

I am working on a magic square program. My program compiles. However, when I enter the square dimension it does not select the correct file. The error says "java.io.FileNotFoundException." It looks like it inserts 0 instead of the entered dimension.

import java.util.*;
import java.io.*;
public class Trial2
{
public static int size, row, col;
public static void main(String[]args)throws java.io.IOException

[Code] ....

View Replies View Related

Buttons To Add And Remove 1 Ball

Apr 29, 2015

1. BALL

import javax.swing.Timer;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Ball extends JPanel {
private int delay = 10;
private Timer timer = new Timer(delay, new TimerListener());
private int x = 0; private int y = 0;

[Code] ...

View Replies View Related

Ball Stopping At The Edge Of Maze

Mar 7, 2015

I am doing an assignment which involves creating a maze and a ball that needs to go through the maze to an end tile then stop the scenario. The maze is made up of 208 buttons in a Jbutton Grid layout. I am replacing certain buttons with a brown tile icon to make up the maze. The ball needs to run along those brown tiles as its path and not be able to move off the brown tile icon images.

I have built the maze and have got quite far with this but now I am stumped on the concept of keeping the ball within the boundary edges and on the brown tiles. I have been told to use if statements, but not had the process explained to me in a way I can understand.

View Replies View Related

Ball Class For Pong Won't Add To JPanel

Jul 28, 2014

I am trying to make a game of pong and am having trouble adding my ball class to a JPanel. Here is what I have so far.

package pong;
import java.awt.*;
public class Ball{
private int xPos,yPos;
public int dx = 5, dy = -5;
public Ball()

[code].....

I want to be able to add this to a JPanel but it is not allowing me to add the ball. Here is my JPanel

package pong;
import java.awt.*;
import javax.swing.JPanel;
public class BallTestPanel

[Code] ....

It reads panel.add(ball); as an error. I says that I need to change the type ball to component.

View Replies View Related

How To Create Multiple Threads Of Ball

Apr 18, 2014

Actually, i want to create multiple ball using multithreading.I have created a class 'CreateBall' and this class is making ball and second class 'Balls' is a Panel where the ball is display.But only one ball is being displayed.

Here is my code.......

CreateBall.java
import java.awt.*;
public class CreateBall implements Runnable
{
int px,py;
int w=20,h=20;
Graphics g;
public CreateBall(int px,int py)

[Code] .....

View Replies View Related

Bouncing Ball Around The Screen - It Keeps Disappearing

Oct 23, 2014

I'm trying to do is get this ball to bounce around the screen and ive gotten so far but not sure how to proceed. The ball keeps disappearing.

//bouncing ball

float x; //ball x position
float dx = 3; //ball x direction is right, step 5
float y; //ball y position
float dy = 3;
void setup () //runs once at start

[Code] ....

View Replies View Related

How To Make Curve Motion With Ball Object

Jun 25, 2013

I want to make a simply game,actualy not a game but I want to moving object curve just like angry bird. Where I can move an object with curve motion. But I don't know how. I just can make it move straight(left,right,down,up).

View Replies View Related

Swing/AWT/SWT :: Making Ball Bounce Around A Screen

Aug 12, 2014

I've got to make a ball bounce around a screen.... From what I can gather they seem to hold back useful java code.

anyway, i've changed the code over and over, and think this is the most succinct way i can manage it. but i have a problem. where i've marked the code (*****) and the following line are conditional on each other. so one won't provide the correct answer without the other line executing first, and vice versa (ignore the code after, i haven't looked at where that should be yet ). Anyway I don't really want to rip the code apart, again, because i'm pretty sure it's close.

View Replies View Related

How To Make Pong Ball Slowly Increase Speed

Apr 11, 2013

I was making a pong game, part copied code off the internet, part my own code, part my friends code. It works well enough, but the ball starts at one speed, and stays the same. I would like to know how to make the ball slowly increase.

It is a java applet, which shouldn't casue any harm right?

My code:

Pong Applet
 
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 public class Pong extends Applet implements MouseMotionListener, KeyListener {
 
[Code] .....

View Replies View Related

Random Color Ball And Move Bottom Bar Vector?

Feb 20, 2014

I created this simple vector java program where ball falling and when collide with the bar below, the ball stops.

I need to make the ball falling randomly from different direction and also need to make the bar move left right using keyboard arrow.

Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
 //using timer to increase the counter

[code].....

View Replies View Related

Ping Pong Game - Ball Drop And Catch

Feb 26, 2014

I have created this ping pong game but having problem with ball and paddle collusion. After first collusion it gives score 1 but later it never touch the paddle.

Code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.Random.*;
import javax.swing.ImageIcon;
 public class Ball extends JFrame implements ActionListener, KeyListener{
int xSpeed=0;

[Code] ....

I think something is wrong here:

public void actionPerformed(ActionEvent e){
y=y+xSpeed;
repaint();
if(y> this.getWidth()){
y=25;
score -=1;
}
if(blockRect.intersects(ballRect)){
y=40;
score +=1;
x =200;
y = 300;
}

View Replies View Related

Add Color To Ball In Pong - Speed Up When Contact Made With Paddle

Apr 23, 2014

I wrote a code from our text book for Pong. Im trying to figure out how to fill the ball with color. Im also wondering how to speed the ball up once there is contact with the paddle. The initial speed is ok but once contact is made with the paddle, it slows down. Makes it too easy to play. Here is my code so far. As far as the game goes, it works and is good. Just wanted to add a couple customizations.

Java Code:

package pong;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;

[Code] .....

View Replies View Related

Counting Scoreboard - How Many Times A Ball Hits Moving Platform In Pong

May 9, 2014

I need to make a scoreboard that can count how many times a ball hits a moving platform in pong and have no clue how to approach this.

View Replies View Related

Java Program - Track Position Of Imaginary Ball As It Bounces Around Imaginary Box

Oct 3, 2014

I am working on a program that asks me to track the position of an imaginary ball as it bounces around an imaginary box. The user gives me input for the starting position of the ball (x,y), the bottom-left boundaries of the box (xl, yl), the top right boundaries (xr, yr) and the angle. The program then needs to find where the ball will hit the box, and then print that location and find the new angle for the next point. My program currently works in most cases if the angle is less than pi.

Here is my code

public class BouncyBall {
public static void main(String args[]){
if (args.length != 8) {
System.out.println("Error: usage is X, Y, Angle, Xl, Yl, Xr, Yr,N");
System.exit(1);
}
double x,y,xl,yl,xr,yr,angle;
int n;
x=Double.parseDouble(args[0]);
y=Double.parseDouble(args[1]);

[Code] .....

View Replies View Related

Web Services :: Code Implementation Generated By Axis2 Code Generator?

Aug 23, 2010

I was a bit confused of the code generated by the Axis2 Code Gen.

I have created a logIn(String username, String password) method in my service and used Axis2 Code Gen to generate the Stub.

I try to access the logIn method from my Client using the Stub like below:

TestServiceStub stub = new TestServiceStub("http://localhost:8080/axis2/services/TestService");
String test = stub.logIn("user","pass").

but instead of UserName and password as the parameters, the Stub created a Login object as the parameter for the logIn method. like the one below:

stub.logIn(Login login1);

I wanted to try the logIn method by providing a static userName and password but it seems impossible because the Stub changed the parameter for the logIn method.

how to test the logIn method.

View Replies View Related

Modify Code That Converts Binary Code To Decimal Numbers

Aug 29, 2014

The code here I have works fine if I just want to ask the user to enter four digits: //java application that asks user to input binary numbers(1 or 0) and convert them to decimal numbers import java.util.Scanner; //program uses class scanner public class binarynumber{
 
//main method that executes the java application
public static void main(String args[]){
//declares variables
 
int digit;
int base=2;
int degree;
double decimal;
int binary_zero=0;
int binary_one=1;
//create scanner for object input

[code]....

The thing is, I want the java application to input more than four digits for the user and I want it to loop it manytimes f until the user ask it to stop.

View Replies View Related

Input Color Code In HSV And Output Equivalent RGB Code

Oct 9, 2014

I need to create a Java program that takes an input a color code in HSV and outputs the equivalent RGB code and Vice versa.

Example:
If you test on the color RED in RGB:
Input: (255,0,0)
Output: (0,100%,100%)
If you test on the color RED in HSV:
Input0,100%,100%)
Output: (255,0,0)

View Replies View Related

Inner If Loop Code

Mar 23, 2015

Can i write inner if loop without { and } if it has more lines of code as below

public class TEst111 {
/**
* @param args
*/
public static void main(String[] args) {
int num=11;
// TODO Auto-generated method stub
if (num < 10)

[code]....

I tested to see outer if which does not need { and } even though it has multiple lines in that

View Replies View Related

Why Should Swing GUI Code Be Placed On EDT

Oct 3, 2014

According to what I read, "when programming in Swing, your GUI creation code should be placed on the Event Dispatch Thread (EDT). This will prevent potential race conditions that could lead to deadlock." (See below for code.)

Why is this? How could making a GUI lead to deadlock?

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}

View Replies View Related

Code To Go From One Web Page To Next

Apr 24, 2014

I have a assignment in which the code has to scrape a web page for a little bit of data including the link to the next page, follow the link, and do this 100 times. It is scraping all the data correctly, including the link, but it isn't following the link to scrape the data on that next page. Instead, it is displaying the first page's data 100 times. I have the code in a while loop where it reads in one character at a time from the page into a string and then uses pattern matching to get the data and the next link from the string. Then it correctly displays the data and should loop back to connect to that next link and read one character at a time into the string and so on.

I have printed the link to the console and it is good. I don't get any errors. I just can't figure out where exactly the problem is. Here is my code:

import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.regex.*;
import javax.swing.* ;
public class SimpleWebSourceGetter{
static void getSourceCode(String url) {
String mystring = "";

[Code] ....

View Replies View Related







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