Projects 3
This page continues my Java projects with a shapes project called Otto, a simple football match simulator, and an extended version of Otto that prints a large letter on a grid.
Project — Otto (Shapes in Java)
Otto is a Shapes class that can represent circles, squares and rectangles with radius, sides, position and fill state. It also explores keeping a ratio between two sides.
public class Shapes {
private double sides;
private double sides1;
private double radius;
private boolean isFilled;
private int xPos;
private int yPos;
private boolean keepRatio = false;
private double currentRatio;
public Shapes(int radius1, boolean isFilled1, int xPos1, int yPos1) {
radius = radius1;
isFilled = isFilled1;
xPos = xPos1;
yPos = yPos1;
}
public Shapes(double pSides, double pSides1, int xPos1, int yPos1, boolean isFilled1){
sides = pSides;
sides1 = pSides1;
xPos = xPos1;
yPos = yPos1;
isFilled = isFilled1;
if (sides1 != 0) {
currentRatio = sides / sides1;
} else {
currentRatio = 0;
}
}
public Shapes(double pSides, int xPos1, int yPos1, boolean isFilled1) {
sides = pSides;
xPos = xPos1;
yPos = yPos1;
isFilled = isFilled1;
}
public double getRadius() {
System.out.println("The current radius is: " + radius);
return radius;
}
public void setRadius(int x) {
radius = x;
System.out.println("New radius: " + radius);
}
public double getSides() {
System.out.println("The current side1 is: " + sides);
return sides;
}
public double getSides1() {
System.out.println("The current side is: " + sides1);
return sides1;
}
public boolean getIsFilled() {
System.out.println("The current state is: " + isFilled);
return isFilled;
}
public void setIsfilled(boolean y){
isFilled = y;
System.out.println("New fill state: " + isFilled);
}
public int xGetPosition() {
System.out.println("The current x position is: " + xPos);
return xPos;
}
public double yGetPosition() {
System.out.println("The current y position is: " + yPos);
return yPos;
}
public void setPosition(int xx , int yy){
xPos = xx;
yPos = yy;
System.out.println("New position: (" + xPos + ", " + yPos + ")");
}
public void sideChange(int zz) {
sides = zz;
System.out.println("New side1 value: " + sides);
}
public void sideChange1(int zzz){
sides1 = zzz;
System.out.println("New side value: " + sides1);
}
public void ratioSame(int x) {
double diff = Math.abs(sides - x);
sides = x;
if (currentRatio != 0) {
sides1 = sides - (diff * currentRatio);
}
System.out.println("New sides after ratioSame side1: " + sides + ", side2: " + sides1);
}
}
public class ShapesRunner
{
public static void main(String[] args)
{
Shapes circle = new Shapes(5, true, 10, 10);
Shapes square = new Shapes(5.6, 10, 20, false);
Shapes rectangle = new Shapes(5.6, 7.3, 6, 9, true);
circle.getRadius();
square.getSides();
rectangle.getSides();
rectangle.getSides1();
rectangle.ratioSame(10);
}
}Video explanation: Click to watch Otto project video
Project — Football Match (Java)
Simulates a football match between two teams, tracking goals and minutes played.
public class FootballMatchRunner
{
public static void main(String[] args)
{
FootballMatch match = new FootballMatch("Hisar High", "River School");
match.advanceTime(15);
match.scoreHomeGoal();
match.advanceTime(20);
match.scoreAwayGoal();
match.advanceTime(10);
match.scoreHomeGoal();
System.out.println("Minutes played: " + match.getMinutesPlayed());
System.out.println("Current score: " + match.getScore());
System.out.println("Winner: " + match.getWinner());
}
}
public class FootballMatch
{
private String homeTeam;
private String awayTeam;
private int homeGoals;
private int awayGoals;
private int minutesPlayed;
public FootballMatch(String pHomeTeam, String pAwayTeam)
{
homeTeam = pHomeTeam;
awayTeam = pAwayTeam;
homeGoals = 0;
awayGoals = 0;
minutesPlayed = 0;
}
public void scoreHomeGoal()
{
homeGoals++;
}
public void scoreAwayGoal()
{
awayGoals++;
}
public void advanceTime(int minutes)
{
minutesPlayed = minutesPlayed + minutes;
}
public String getScore()
{
return homeTeam + " " + homeGoals + " - " + awayGoals + " " + awayTeam;
}
public int getMinutesPlayed()
{
return minutesPlayed;
}
public String getWinner()
{
if (homeGoals > awayGoals)
{
return homeTeam;
}
else if (awayGoals > homeGoals)
{
return awayTeam;
}
else
{
return "Draw";
}
}
}Video: Click to watch explanation
Project — Otto (Grid U Printer)
This updated Otto project still uses the Shapes class, but now works together with a
printing class that uses a 40x40 grid to draw a large letter (by default a big "U")
made of any character you choose.
public class Shapes {
private double sides;
private double sides1;
private double radius;
private boolean isFilled;
private int xPos;
private int yPos;
private boolean keepRatio = false;
private double currentRatio;
//Constructer for creating circle
public Shapes(int radius1, boolean isFilled1, int xPos1, int yPos1) {
radius = radius1;
isFilled = isFilled1;
xPos = xPos1;
yPos = yPos1;
}
//Construction for creating rectangle
public Shapes(double pSides, double pSides1, int xPos1, int yPos1, boolean isFilled1){
sides = pSides;
sides1 = pSides1;
xPos = xPos1;
yPos = yPos1;
isFilled = isFilled1;
if (sides1 != 0) {
currentRatio = sides / sides1;
} else {
currentRatio = 0;
}
}
//Construction for creating square
public Shapes(double pSides, int xPos1, int yPos1, boolean isFilled1) {
sides = pSides;
xPos = xPos1;
yPos = yPos1;
isFilled = isFilled1;
}
public double getRadius() {
System.out.println("The current radius is: " + radius);
return radius;
}
public void setRadius(int x) {
radius = x;
System.out.println("New radius: " + radius);
}
public double getSides() {
System.out.println("The current side1 is: " + sides);
return sides;
}
public double getSides1() {
System.out.println("The current side is: " + sides1);
return sides1;
}
public boolean getIsFilled() {
System.out.println("The current state is: " + isFilled);
return isFilled;
}
public void setIsfilled(boolean y){
isFilled = y;
System.out.println("New fill state: " + isFilled);
}
public int xGetPosition() {
System.out.println("The current x position is: " + xPos);
return xPos;
}
public double yGetPosition() {
System.out.println("The current y position is: " + yPos);
return yPos;
}
public void setPosition(int xx , int yy){
xPos = xx;
yPos = yy;
System.out.println("New position: (" + xPos + ", " + yPos + ")");
}
public void sideChange(int zz) {
sides = zz;
System.out.println("New side1 value: " + sides);
}
public void sideChange1(int zzz){
sides1 = zzz;
System.out.println("New side value: " + sides1);
}
public void ratioSame(int x) {
double diff = Math.abs(sides - x);
sides = x;
if (currentRatio != 0) {
sides1 = sides - (diff * currentRatio);
}
System.out.println("New sides after ratioSame side1: " + sides + ", side2: " + sides1);
}
}
public class ShapesRunner {
public static void main(String[] args) {
Shapes circle = new Shapes(5, true, 10, 50);
printing.printName("A", "b");
printing.printPlane();
}
}
public class printing {
private static String[][] grid = new String[40][40];
public static void printName(String uCharInput, String bgCharInput) {
if (uCharInput.length() == 0) {
uCharInput = "U";
}
if (bgCharInput.length() == 0) {
bgCharInput = " ";
}
String uChar = uCharInput.substring(0, 1) + " ";
String bgChar = bgCharInput.substring(0, 1) + " ";
fillGrid(bgChar);
drawU(uChar);
}
private static void fillGrid(String bg) {
for (int y = 0; y < 40; y++) {
for (int x = 0; x < 40; x++) {
grid[x][y] = bg;
}
}
}
private static void setGrid(int x, int y, String value) {
if (x >= 0 && x < 40 && y >= 0 && y < 40) {
grid[x][y] = value;
}
}
private static void drawU(String uChar) {
int leftX = 8;
int rightX = 16;
int topY = 5;
int bottomY = 25;
for (int y = topY; y < bottomY; y++) {
setGrid(leftX, y, uChar);
setGrid(rightX, y, uChar);
}
for (int x = leftX; x <= rightX; x++) {
setGrid(x, bottomY, uChar);
}
}
public static void printPlane() {
for (int y = 0; y < 40; y++) {
for (int x = 0; x < 40; x++) {
System.out.print(grid[x][y]);
}
System.out.println();
}
}
}