JAVA PROGRAMMING
It is a well-researched fact that men in a restroom generally
prefer to maximize their distance from already occupied stalls, by
occupying the middle of the longest sequence of unoccupied
places.
For example, consider the situation where ten stalls are empty. _ _
_ _ _ _ _ _ _ _
The first visitor will occupy a middle position: _ _ _ _ _ X _ _ _
_
The next visitor will be in the middle of the empty area at the
left. _ _ X _ _ X _ _ _ _
Write a program that reads the number of the stalls from the user
in the RestroomSimulation.java file and then prints out the
diagrams in the format given above when the stalls become filled,
one at a time. Hint: Use an array of boolean values to indicate
whether a stall is occupied. The user should enter a number between
5 and 30 for the number of stalls. The number should be validated
to be within that range. The rest of the code will be written in
the Restroom.java file.
There are two other test programs that you can use to test your
program included in the zip file. They are the ones call Restroom
Tester.java and RestroomTester2.java.
**RESTROOM.JAVA**
/**
A class that shows how restroom stalls are occupied.
*/
public class Restroom
{
. . .
/**
Constructs a restroom with a given number of stalls.
@param ns the number of stalls
*/
public Restroom(int ns)
{
. . .
}
/*
Adds an occupant in the middle of the longest sequence of
unoccupied places.
*/
public void addOccupant()
{
. . .
}
/*
Gets a string describing the current stall occupation
@return a string with _ for an empty stall and X for an occupied
one
*/
public String getStalls()
{
. . .
}
}
**RESTROOMSIMULATION.JAVA**
/**
Print diagrams of restroom stalls as they are occupied.
The premise is that people generally prefer to maximize
their distance from already occupied stalls, by occupying
the middle of the longest sequence of unoccupied places.
*/
public class RestroomSimulation
{
public static void main(String[] args)
{
int STALLS = 10;
Restroom wc = new Restroom(STALLS);
for (int i = 1; i <= STALLS; i++)
{
wc.addOccupant();
System.out.println(wc.getStalls());
}
}
}
**RESTROOMTESTER.JAVA**
public class RestroomTester
{
public static void main(String[] args)
{
int STALLS = 12;
Restroom wc = new Restroom(STALLS);
wc.addOccupant();
System.out.println(wc.getStalls());
System.out.println("Expected: ______X_____");
wc.addOccupant();
System.out.println(wc.getStalls());
System.out.println("Expected: ___X__X_____");
}
}
**RESTROOMTESTER2**
//HIDE
public class RestroomTester2
{
public static void main(String[] args)
{
int STALLS = 12;
Restroom wc = new Restroom(STALLS);
wc.addOccupant();
System.out.println(wc.getStalls());
System.out.println("Expected: ______X_____");
wc.addOccupant();
System.out.println(wc.getStalls());
System.out.println("Expected: ___X__X_____");
wc.addOccupant();
System.out.println(wc.getStalls());
System.out.println("Expected: ___X__X__X__");
wc.addOccupant();
System.out.println(wc.getStalls());
System.out.println("Expected: _X_X__X__X__");
wc.addOccupant();
System.out.println(wc.getStalls());
System.out.println("Expected: _X_X_XX__X__");
wc.addOccupant();
System.out.println(wc.getStalls());
System.out.println("Expected: _X_X_XX_XX__");
wc.addOccupant();
System.out.println(wc.getStalls());
System.out.println("Expected: _X_X_XX_XX_X");
wc.addOccupant();
System.out.println(wc.getStalls());
System.out.println("Expected: XX_X_XX_XX_X");
wc.addOccupant();
System.out.println(wc.getStalls());
System.out.println("Expected: XXXX_XX_XX_X");
}
}
--------------------------------------------------------------------------------------------------
// Restroom.java file
public class Restroom {
/**
Constructs a restroom with a given number of
stalls.
@param ns the number of stalls
*/
boolean occupiedRestrooms[];
public Restroom(int ns)
{
occupiedRestrooms = new
boolean[ns];
for(int i = 0; i < ns; i++)
{
occupiedRestrooms[i] = false;
}
}
/*
Adds an occupant in the middle of the longest sequence
of
unoccupied places.
*/
public void addOccupant()
{
//find the longest unoccupied
sequence
int longestSeqCount = 0;
int longestSeqIndex = -1;
int currentSeqCount = 0;
int n =
occupiedRestrooms.length;
for(int i = 0; i < n ; i++)
{
if(occupiedRestrooms[i] == true) {
if(longestSeqCount < currentSeqCount) {
longestSeqCount =
currentSeqCount;
longestSeqIndex = i -
currentSeqCount;
}
currentSeqCount = 0;
}else {
currentSeqCount += 1;
}
}
if(longestSeqCount <
currentSeqCount) {
longestSeqCount
= currentSeqCount;
longestSeqIndex
= n - currentSeqCount;
}
//Set the middle of the longest
Unoccupied sequence to True
occupiedRestrooms[longestSeqIndex + (longestSeqCount/2)] =
true;
}
/*
Gets a string describing the current stall
occupation
@return a string with _ for an empty stall and X for
an occupied one
*/
public String getStalls()
{
String stall = "";
int n =
occupiedRestrooms.length;
for(int i = 0;i<n;i++) {
if(occupiedRestrooms[i] == true) {
stall = stall.concat("X ");
}else {
stall = stall.concat("_ ");
}
}
return stall;
}
}
--------------------------------------------------------------------------------------------------
// Screenshot for n= 12 stalls where 12 occupants are added one by one
// We basically set STALLS = 12 in RESTROOMSIMULATION.JAVA file and then run that file
--------------------------------------------------------------------------------------------------
JAVA PROGRAMMING It is a well-researched fact that men in a restroom generally prefer to maximize...
I need help with java It is a well-researched fact that men in a restroom generally prefer to maximize their distance from already occupied stalls, by occupying the middle of the longest sequence of unoccupied places. For example, consider the situation where ten stalls are empty. __________ The first visitor will occupy a middle position: _____X____ The next visitor will be in the middle of the empty area at the left. __X__X____ Write a program that reads the number of...
Java will be printed 10. can you explain step by step why? public class WhatsPrinted2 { public static void whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) { B[i]=A[i]*2; } A=B; } public static void main(String args[]) { int A[] = {10,20,30}; whatHappens(A); System.out.println(A[0]); } } will print 10. explain how it's works. Thanks public class WhatsPrinted3 { public static int[] whatHappens(int A[]) { int []B = new int[A.length]; for (int i=0; i<A.length; i++) {...
Intro to Programming in Java Elements of Programming Book Problem 1.3.25 1.3.25 .) Modify Gambler to take an extra command-line argument that specifies the (fixed) probability that the gambler wins each bet. Use your program to try to learn how this probability affects the chance of winning and the expected number of bets. Try a value of p close to 0.5 (say, 0.48) public class Gambler { public static void main(String[] args) { int stake = Integer.parseInt(args[0]); int...
Solve this using Java for an Intro To Java Class.
Provided files:
Person.java
/**
* Models a person
*/
public class Person
{
private String name;
private String gender;
private int age;
/**
* Consructs a Person object
* @param name the name of the person
* @param gender the gender of the person either
* m for male or f for female
* @param age the age of the person
*/
public Person(String name, String gender, int age)
{...
help me with a question in java: class A { int i = 10; } class B extends A { int i = 20; } public class Boo { public static void main(String[] args) { A a = new B(); System.out.println(a.i); } }
Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...
What display is produced by the execution of this JAVA program if we make the following call f(0,0). Please explain your answer public class function { static void f(int x){ System.out.println(1); } static void f(double x){ System.out.println(2); } static void f(char x){ System.out.println(3); } static void f(long x){ System.out.println(4); } public static void main(String[] args) { } }
Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...
the answer must be in java. thank you
Question 2 [8 points] Consider the class Class A below: class Class A implements Serializable{ int a; static int b; transient int c; String s; // Constructor public Class A (int a, int b, int c, Strings){ this.a = a; this.b = b; this.c = c; this.s = s; Complete the class Test to test the serialization and deserialization of the objects of class Class A. State the possible variable values following...
Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....