Skip to content

Instantly share code, notes, and snippets.

View ahmednasserpro's full-sized avatar

ahmednasserpro

View GitHub Profile
@ahmednasserpro
ahmednasserpro / AnimationSelfAvoidingRandomWalk.java
Created May 26, 2019 15:04
Animation self-avoiding random walk
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class AnimationSelfAvoidingRandomWalk extends JFrame {
private static final int N = 16;
private Point[][] lattice = new Point[N + 1][N + 1];
private int i = (N + 1) / 2;
private int j = (N + 1) / 2;
@ahmednasserpro
ahmednasserpro / HitBalloonGame.java
Created May 25, 2019 21:00
HitBalloonGame.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HitBalloonGame extends JFrame {
private ShootPanel p = new ShootPanel();
HitBalloonGame() {
add(p);
@ahmednasserpro
ahmednasserpro / BigIntegerRational.java
Created May 19, 2019 00:32
The Rational class with unlimited numbers to process
class Rational extends Number implements Comparable<Rational> {
// Data fields for numerator and denominator
private BigInteger numerator = BigInteger.ZERO;
private BigInteger denominator = BigInteger.ONE;
/** Construct a rational with default properties */
public Rational() {
this(BigInteger.ZERO, BigInteger.ONE);
}
@ahmednasserpro
ahmednasserpro / Rational.java
Created May 14, 2019 03:15
The Rational class is used for rational number. A rational number consists of a numerator and a denominator. There are many equivalent rational numbers—for example, 1/3 = 2/6 = 3/9 = 4/12. The numerator and the denominator of 1/3 have no common divisor except 1
/**
* The {@link Rational} class is used for rational number.
* A rational number consists of a numerator and a denominator.
* There are many equivalent rational numbers—for example,
* 1/3 = 2/6 = 3/9 = 4/12.
* The numerator and the denominator of 1/3 have no common divisor except 1
*
* @author AhmedNasser
*/
public class Rational extends Number implements Comparable<Rational> {
@ahmednasserpro
ahmednasserpro / Exercise14_21.java
Last active May 9, 2019 03:47
(Display a graph) A graph consists of vertices and edges that connect vertices. Write a program that reads a graph from a file and displays it on a panel. The first line in the file contains a number that indicates the number of vertices (n). The vertices are labeled as 0, 1, . . . , n-1. Each subsequent line, with the format u x y v1 v2 ..., de…
import java.awt.Color;
import java.awt.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
public class Test {
public static void main(String[] args) throws Exception {
@ahmednasserpro
ahmednasserpro / Exercise14_17.java
Created May 6, 2019 22:54
***14.17 (Game: hangman) Rewrite Exercise 9.25. The program reads the words stored in a text file named hangman.txt. Words are delimited by spaces.
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
File file = new File("hangman.txt");
Scanner input = new Scanner(file);
List<String> words = new ArrayList<>();
@ahmednasserpro
ahmednasserpro / Exercise13_11.java
Last active April 22, 2019 20:55
13.11 (Plot the square function) Write a program that draws a diagram for the function f(x) = x2 (see Figure 13.29c).
/*
* *13.11 (Plot the square function) Write a program that draws a diagram for the function
* f(x) = x2 (see Figure 13.29c).
*
* Hint: Add points to a polygon p using the following loop:
*
* double scaleFactor = 0.1;
* for (int x = -100; x <= 100; x++) {
* p.addPoint(x + 200, 200 - (int)(scaleFactor * x * x));
* }