please write down both java and output. Thank you!
Here is the code for the question with output. In case of any issues, post a comment. If happy with the answer, please rate it. Thank you
Chip.java
public class Chip {
public static final String RED = "RED";
public static final String YELLOW = "YELLOW";
private String colour;
public Chip(String c)
{
colour = c;
}
public boolean equals(Chip c)
{
if(c == null)
return false;
if(c.colour.equalsIgnoreCase(colour))
return true;
else
return false;
}
public void setColour(String c)
{
colour = c;
}
public String getColour()
{
return colour;
}
public String toString()
{
return colour.charAt(0)+"";
}
}
Board.java
public class Board {
private int rows;
private int cols;
protected Chip[][] board;
public Board(int r, int c)
{
rows = r;
cols = c;
board = new Chip[rows][cols];
}
public int getRows()
{
return rows;
}
public int getCols()
{
return cols;
}
public boolean isEmpty(int r, int c)
{
return (board[r][c] == null);
}
public boolean add(int r, int c, Chip chip)
{
if(isEmpty(r, c))
{
board[r][c] = chip;
return true;
}
else
return false;
}
}
Connect4Board.java
public class Connect4Board extends Board {
private Chip winner;
public Connect4Board()
{
super(6, 7);
}
public boolean add(int c, String colour)
{
//check for empty row from bottom in the column c
for(int r = 5; r >= 0; r--)
{
if(isEmpty(r, c))
{
add(r, c, new Chip(colour));
return true;
}
}
return false;
}
public String winType()
{
int total = 0;
for( int r = 0; r < getRows(); r++)
{
for(int c = 0; c < getCols(); c++)
{
if(board[r][c] == null) continue;
winner = board[r][c];
//for each of the current location of r,c , check 8 different directions to see if
//4 were connected in that direction
//check +ve x horizontal direction
total = 0;
for(int i = 0; i < 4 && c+i < getCols(); i++)
{
if(board[r][c].equals(board[r][c+i]) )
total++;
}
if(total == 4)
return "Horizontal";
//check -ve horizontal direction
total = 0;
for(int i = 0; i < 4 && c-i >= 0; i++)
{
if(board[r][c].equals(board[r][c-i] ))
total++;
}
if(total == 4)
return "Horizontal";
//check +ve vertical direction
total = 0;
for(int i = 0; i < 4 && r-i >= 0; i++)
{
if(board[r][c].equals(board[r - i][c] ))
total++;
}
if(total == 4)
return "Vertical";
//check -ve vertical direction
total = 0;
for(int i = 0; i < 4 && r+i < getRows(); i++)
{
if(board[r][c].equals(board[r + i][c]) )
total++;
}
if(total == 4)
return "Vertical";
//check diagonal 1 quadrant
total = 0;
for(int i = 0; i < 4 && r-i >= 0 && c+i < getCols(); i++)
{
if(board[r][c].equals(board[r - i][c + i] ))
total++;
}
if(total == 4)
return "Diagonal";
//check diagonal 2nd quadrant
total = 0;
for(int i = 0; i < 4 && r - i >= 0 && c - i >= 0; i++)
{
if(board[r][c].equals(board[r - i][c - i] ))
total++;
}
if(total == 4)
return "Diagonal";
//check diagonal 3rd quadrant
total = 0;
for(int i = 0; i < 4 && r + i < getRows() && c - i >= 0; i++)
{
if(board[r][c].equals(board[r + i][c - i] ))
total++;
}
if(total == 4)
return "Diagonal";
//check diagonal 4th quadrant
total = 0;
for(int i = 0; i < 4 && r + i < getRows() && c + i < getCols(); i++)
{
if(board[r][c].equals(board[r + i][c + i] ))
total++;
}
if(total == 4)
return "Diagonal";
}
}
winner = null;
return null;
}
public boolean winner()
{
if(winType() == null)
return false;
else
return true;
}
public boolean isFull()
{
for(int i = 0; i < getRows(); i++)
{
for(int j = 0; j < getCols(); j++)
if(board[i][j] == null)
return false;
}
return true;
}
public Chip getWinningChip()
{
return winner;
}
public String toString()
{
String str ="\n";
for(int i = 1 ; i <= getCols(); i ++)
str += "\t" + i;
for(int r = 0; r < getRows(); r++)
{
str += "\n" + (r+1) ;
for(int c = 0; c < getCols(); c++)
{
str += "\t" ;
if(board[r][c] == null)
str += " ";
else
str += board[r][c];
}
str += "\n";
}
return str;
}
}
Play.java
import java.util.Scanner;
public class Play {
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
String ans;
do
{
String player1, player2;
int totalTurns = 0;
int currTurn = 0;
System.out.println("Welcome to Connect 4. Please enter your names.");
System.out.print("Player 1 name: ");
player1 = keybd.nextLine().trim();
System.out.print("Player 2 name: ");
player2 = keybd.nextLine().trim();
System.out.println(player1 + " - You have red chips \"R\" and you go first");
System.out.println(player2 + " - You have yellow chips \"Y\" and you go second");
Connect4Board board = new Connect4Board();
String currChip = Chip.RED;
int col;
while(!board.isFull() && board.winner() == false)
{
System.out.println(board);
do{
System.out.print("\n"+ currChip + " - Please input a column # between 1-7: ");
col = keybd.nextInt();
}while(col < 1 || col > 7);
if(board.add(col - 1, currChip))
{
totalTurns++;
//change turn and colours
if(currChip.equals(Chip.RED))
currChip = Chip.YELLOW;
else
currChip = Chip.RED;
}
else
System.out.println("Chip could not be added in the column. Try another column!");
}
Chip winner = board.getWinningChip();
if(winner == null)
System.out.println("There is no winner");
else
{
if(winner.getColour() == Chip.RED)
System.out.println("RED - Connect 4 ! Congratulations " + player1 + "! You win in " + totalTurns + " turns");
else
System.out.println("YELLOW - Connect 4 ! Congratulations " + player2 + "! You win in " + totalTurns + " turns");
}
System.out.print("\n\nDo you want to play again ? y/n: ");
ans = keybd.next().trim();
}while(ans.equalsIgnoreCase("y"));
}
}
output
Welcome to Connect 4. Please enter your names.
Player 1 name: Bob
Player 2 name: Jane
Bob - You have red chips "R" and you go first
Jane - You have yellow chips "Y" and you go second
1 2 3 4 5 6 7
1
2
3
4
5
6
RED - Please input a column # between 1-7: 1
1 2 3 4 5 6 7
1
2
3
4
5
6 R
YELLOW - Please input a column # between 1-7: 2
1 2 3 4 5 6 7
1
2
3
4
5
6 R Y
RED - Please input a column # between 1-7: 1
1 2 3 4 5 6 7
1
2
3
4
5 R
6 R Y
YELLOW - Please input a column # between 1-7: 1
1 2 3 4 5 6 7
1
2
3
4 Y
5 R
6 R Y
RED - Please input a column # between 1-7: 3
1 2 3 4 5 6 7
1
2
3
4 Y
5 R
6 R Y R
YELLOW - Please input a column # between 1-7: 2
1 2 3 4 5 6 7
1
2
3
4 Y
5 R Y
6 R Y R
RED - Please input a column # between 1-7: 1
1 2 3 4 5 6 7
1
2
3 R
4 Y
5 R Y
6 R Y R
YELLOW - Please input a column # between 1-7: 2
1 2 3 4 5 6 7
1
2
3 R
4 Y Y
5 R Y
6 R Y R
RED - Please input a column # between 1-7: 4
1 2 3 4 5 6 7
1
2
3 R
4 Y Y
5 R Y
6 R Y R R
YELLOW - Please input a column # between 1-7: 2
YELLOW - Connect 4 ! Congratulations Jane! You win in 10 turns
Do you want to play again ? y/n: n
please write down both java and output. Thank you! Chip - colour String Chip (e: String):...
Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...
You are to fully program the List interface using Java generics and Javadoc. Please note - an interface doesn't account for implementation. It is just the function definitions and Javadoc. The following functions need to be present - please post screenshot of the code with these functions implemented boolean add(E e) void add(int index, E element) void clear() boolean contains(Object o) boolean equals(Object o) E get(int index) int indexOf(Object o) boolean isEmpty() int lastIndexOf(Object o) E remove(int index) boolean remove(Object...
Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...
Please use Java programming: Modify both ArrayList and LinkedList classes and add the following method to both classes: public void reverseThisList(), This method will reverse the lists. When testing the method: print out the original list, call the new method, then print out the list again ------------------------------------------------------------------------- //ARRAY LIST class: public class ArrayList<E> implements List<E> { /** Array of elements in this List. */ private E[] data; /** Number of elements currently in this List. */ private int size; /**...
This needs to be done in Java. Please use basic Java coding at an intro level. THANK YOU FOR YOUR HELP! I WILL LEAVE A GOOD RATING! Design and implement MoneyTest and Money. Use the test-first approach, as we discussed, and document each method before implementing. Money - dollars : int - cents : int + Money (int, int) + getDollars() + getCents() + add (Money) : Money + subtract (Money) : Money + toString() : String + equals (Money)...
java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...
no buffered reader. no try catch statements. java code please. And using this super class: Medialtemjava a Create the "middle" of the diagram, the DVD and ForeignDVD classes as below: The DVD class will have the following attributes and behaviors: Attributes (in addition to those inherited from Medialtem): • director:String • rating: String • runtime: int (this will represent the number of minutes) Behaviors (methods) • constructors (at least 3): the default, the "overloaded" as we know it, passing in...
Hello I am having trouble with a connectFour java program. this issue is in my findLocalWinner method, it declares a winner for horizontal wins, but not for vertical. if anyone can see what im doing wrong. public class ConnectFour { /** Number of columns on the board. */ public static final int COLUMNS = 7; /** Number of rows on the board. */ public static final int ROWS = 6; /** Character for computer player's pieces */ public static final...
Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { } //get method (Accessor Methods ) public double getRadius (...
using CSCI300 java Given the following UML Class Diagram Club_Card # card_num : String # name : String # age: int # year: int + Club_Card (all parameters) + Setters & Getters + toString() : String + equals(x: Object): boolean [10 pts] Define the class Club_Card, which contains: 1. All arguments constructor which accepts all required arguments from the main and instantiate a Club_Card object. 2. Set & get method for each data attribute. 3. A toString() method which returns...