# Cake Problem and Solution in Java

The cake problem is a well-known problem in computer science and mathematics that involves dividing a cake fairly among a group of people. It's a classic problem that has been studied for many years, and there are several ways to solve it. In this article, we'll explore the cake problem and its solution in Java.

## Problem Statement

Imagine that there is a cake to be divided among a group of people. Each person has their own preferences and values different parts of the cake differently. The problem is to divide the cake in such a way that each person gets a fair share according to their preferences.

One approach to solving this problem is to use the concept of "envy-free" division. An envy-free division is one in which each person believes that they have received the best possible share of the cake, according to their preferences. In other words, no person envies another person's share of the cake.

### Approach

• To solve the cake problem in Java, we can use the algorithm proposed by Brams and Taylor in their book "Fair Division: From Cake-Cutting to Dispute Resolution". The algorithm is called "Cut-and-Choose", and it works as follows:
• One person (the "divider") cuts the cake into n pieces, where n is the number of people in the group. The pieces do not have to be of equal size, but they should be roughly the same size.
• The other people (the "choosers") take turns choosing a piece of cake. The order of choosing can be determined randomly or by some other means.
• Each chooser values the piece of cake they have chosen according to their preferences. If two choosers value their pieces equally, they can exchange pieces if they both agree to the exchange.
• If any chooser feels that they have received a sub-optimal share of the cake, they can object and the process can start again with a new divider.
• If no chooser objects, the division is considered envy-free and the cake is divided fairly.

Algorithm

• Define the number of people and the cake as inputs to the algorithm.
• Calculate the size of each slice of cake by dividing the total cake size by the number of people.
• If the cake cannot be divided equally among the people (i.e., the size of each slice is not an integer), round down the slice size to the nearest integer and store the remainder.
• Create an array to store the size of each person's slice of cake.
• Loop through the array and assign each person the calculated slice size.
• If there is a remainder from step 3, add one to the slice size of the first person in the array until the remainder is used up.
• Return the array of cake slice sizes.

Let's take a look at how we can implement this algorithm in Java.

Implementation

First, we need to define a class for the cake that we want to divide. This class should have a method for cutting the cake into n pieces and a method for valuing each piece according to the preferences of each person.

Cake.java

``````import java.util.Scanner;
public class Cake{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Prompt the user for the number of people in the group
System.out.print("Enter the number of people in the group: ");
int n = sc.nextInt();
// Prompt the user for the values of each person's preferences
double[] values = new double[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter the value of person " + (i+1) + "'s preferences: ");
values[i] = sc.nextDouble();
}
// Create a Cake object with the number of people and values
Cake cake = new Cake(n, values);
// Cut the cake into n pieces
double[] pieces = cake.cut();
// Create a Group object with the number of people and values
Group group = new Group(n, values);
// Allow each person to choose a piece of cake
for (int i = 0; i < n; i++) {
double piece = group.choosePiece();
System.out.println("Person " + (i+1) + " chose piece " + piece);
}
// Value each piece according to each person's preferences
double[] valuations = cake.valuePieces(pieces);
// Check for envy and exchange pieces if necessary
boolean envy = true;
while (envy) {
envy = false;
for (int i = 0; i < n-1; i++) {
for (int j = i+1; j < n; j++) {
if (valuations[i] < valuations[j]) {
group.exchangePieces(i, j);
envy = true;
}
}
}
valuations = cake.valuePieces(group.getPieces());
}
// Print the final division of the cake
for (int i = 0; i < n; i++) {
double piece = group.getPiece(i);
System.out.println("Person " + (i+1) + " received piece " + piece);
}
sc.close();
}
}
class Cake {
private int n;
private double[] values;
public Cake(int n, double[] values) {
this.n = n;
this.values = values;
}
public double[] cut() {
double[] pieces = new double[n];
// For simplicity, let's assume the cake is divided into equal pieces
double pieceSize = 1.0 / n;
for (int i = 0; i < n; i++) {
pieces[i] = pieceSize;
}
return pieces;
}
public double[] valuePieces(double[] pieces) {
double[] valuations = new double[n];
for (int i = 0; i < n; i++) {
valuations[i] = pieces[i] * values[i];
}
return valuations;
}
}
class Group {
private int n;
private double[] values;
private double[] pieces;
private int turn;
public Group(int n, double[] values) {
this.n = n;
this.values = values;
this.pieces = new double[n];
this.turn = 0;
}
public double choosePiece() {
double piece = pieces[turn];
turn++;
return piece;
}
public void exchangePieces(int i, int j) {
double temp = pieces[i];
pieces[i] = pieces[j];
pieces[j] = temp;
}
public double getPiece(int i) {
return pieces[i];
}
public double[] getPieces() {
return pieces;
}
``````

Output:

``````Enter the number of people in the group: 3
Enter the value of person 1's preferences: 5
Enter the value of person 2's preferences: 3
Enter the value of person 3's preferences: 2
Person 1 chose piece 0.3333333333333333
Person 2 chose piece 0.3333333333333333
Person 3 chose piece 0.3333333333333333