Complete MP5

master
Chloe Fontenot 🏳️‍⚧️ 2022-11-26 15:25:49 +07:00
parent bd3ec4335e
commit 0a76658f85
6 changed files with 184 additions and 10 deletions

1
.gitignore vendored

@ -43,3 +43,4 @@
/Assignments/Experiments/mavenproject1/target/
/Assignments/lab16_arrays2_CalebFontenot/target/
/Assignments/lab17_CalebFontenot/target/
/Assignments/MP5_CalebFontenot/target/

@ -0,0 +1,113 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>MP5_CalebFontenot.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #000000; background-color: #ffffff; font-family: monospace}
pre {color: #000000; background-color: #ffffff; font-family: monospace}
table {color: #404040; background-color: #e9e8e2; font-family: monospace}
.ST1 {color: #969696; font-family: monospace; font-weight: bold}
.ST2 {font-family: monospace; font-weight: bold}
.comment {color: #969696}
.ST4 {font-family: monospace; font-weight: bold; font-style: italic}
.ST7 {font-family: monospace; font-style: italic}
.ST3 {color: #ce54b8; font-family: monospace; font-style: italic}
.ST0 {color: #287bde}
.ST6 {color: #ce54b8}
.string {color: #1e9347}
.ST5 {color: #1e9347; font-family: monospace; font-weight: bold}
.literal {color: #336bdd}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/MP5_CalebFontenot.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/$</span><span class="comment">{packagePath}/${mainClassName}.java to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5_calebfontenot;
<span class="literal">import</span> java.util.Scanner;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> <span class="ST2">MP5_CalebFontenot</span> {
<span class="comment">// Toggles execution of the printArray method.</span>
<span class="literal">static</span> <span class="literal">final</span> <span class="literal">boolean</span> <span class="ST3">debug</span> = <span class="literal">false</span>;
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">printArray</span>(<span class="literal">boolean</span>[] array) {
<span class="comment">// Color codes</span>
<span class="literal">final</span> String ANSI_RED = <span class="string">&quot;</span><span class="ST5">\u001B</span><span class="string">[31m</span><span class="string">&quot;</span>;
<span class="literal">final</span> String ANSI_GREEN = <span class="string">&quot;</span><span class="ST5">\u001B</span><span class="string">[32m</span><span class="string">&quot;</span>;
<span class="literal">final</span> String ANSI_WHITE = <span class="string">&quot;</span><span class="ST5">\u001B</span><span class="string">[37m</span><span class="string">&quot;</span>;
<span class="comment">// Print the array</span>
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt;= array.<span class="ST6">length</span> - 1; i++) {
<span class="literal">if</span> (array[i]) {
System.<span class="ST3">out</span>.print(ANSI_GREEN + array[i] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
} <span class="literal">else</span> {
System.<span class="ST3">out</span>.print(ANSI_RED + array[i] + <span class="string">&quot;</span> <span class="string">&quot;</span>);
}
<span class="literal">if</span> (i != 0) {
<span class="literal">if</span> ((i + 1) % 10 == 0) {
System.<span class="ST3">out</span>.println(ANSI_WHITE);
}
}
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">solveAndPrint</span>(<span class="literal">boolean</span>[] array, <span class="literal">int</span> studentCount) {
<span class="literal">for</span> (<span class="literal">int</span> student = 1; student &lt;= studentCount; student++) {
<span class="literal">if</span> (<span class="ST3">debug</span>) {
System.<span class="ST3">out</span>.println(<span class="string">&quot;</span><span class="string">Student </span><span class="string">&quot;</span> + student + <span class="string">&quot;</span><span class="string"> is interacting with the lockers!</span><span class="string">&quot;</span>);
}
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt;= array.<span class="ST6">length</span> - 1; i++) {
<span class="literal">if</span> ((i + 1) % student == 0) {
array[i] = !array[i];
}
}
<span class="literal">if</span> (<span class="ST3">debug</span>) {
<span class="ST7">printArray</span>(array);
}
}
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i &lt; array.<span class="ST6">length</span>; i++) {
<span class="literal">if</span> (array[i]) {
System.<span class="ST3">out</span>.println(<span class="string">&quot;</span><span class="string">Locker </span><span class="string">&quot;</span> + (i + 1) + <span class="string">&quot;</span><span class="string"> is open.</span><span class="string">&quot;</span>);
}
}
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST4">menu</span>() {
<span class="comment">// Create scanner</span>
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST3">in</span>);
<span class="literal">int</span> userInput;
<span class="literal">do</span> {
System.<span class="ST3">out</span>.print(<span class="string">&quot;</span><span class="string">Enter 0 to quit, and 1 to enter the number of students: </span><span class="string">&quot;</span>);
userInput = input.nextInt();
<span class="literal">if</span> (userInput == 1) {
System.<span class="ST3">out</span>.print(<span class="string">&quot;</span><span class="string">Enter the number of students: </span><span class="string">&quot;</span>);
userInput = input.nextInt();
<span class="literal">break</span>;
}
} <span class="literal">while</span> (userInput != 0);
<span class="literal">return</span> userInput;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">main</span>(String[] args) {
<span class="literal">final</span> <span class="literal">int</span> N = <span class="ST7">menu</span>();
<span class="literal">boolean</span>[] lockers = <span class="literal">new</span> <span class="literal">boolean</span>[N];
<span class="comment">// Execute solve and print</span>
<span class="ST7">solveAndPrint</span>(lockers, N);
}
}
</pre></body>
</html>

@ -7,8 +7,8 @@
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<exec.mainClass>com.calebfontenot.mp5_calebfontenot.MP5_CalebFontenot</exec.mainClass>
</properties>
</project>

@ -0,0 +1,2 @@
#!/bin/bash
mvn exec:java -Dexec.mainClass="com.calebfontenot.mp5_calebfontenot.MP5_CalebFontenot"

@ -2,24 +2,82 @@
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
*/
package com.calebfontenot.mp5_calebfontenot;
import java.util.Scanner;
/**
*
* @author caleb
*/
public class MP5_CalebFontenot {
// Toggles execution of the printArray method.
static final boolean debug = true;
public static void solveAndPrint(boolean[] ar) {
public static void printArray(boolean[] array) {
// Color codes
final String ANSI_RED = "\u001B[31m";
final String ANSI_GREEN = "\u001B[32m";
final String ANSI_WHITE = "\u001B[37m";
// Print the array
for (int i = 0; i <= array.length - 1; i++) {
if (array[i]) {
System.out.print(ANSI_GREEN + array[i] + " ");
} else {
System.out.print(ANSI_RED + array[i] + " ");
}
if (i != 0) {
if ((i + 1) % 10 == 0) {
System.out.println(ANSI_WHITE);
}
}
}
}
public static int[] menu() {
public static void solveAndPrint(boolean[] array, int studentCount) {
for (int student = 1; student <= studentCount; student++) {
if (debug) {
System.out.println("Student " + student + " is interacting with the lockers!");
}
for (int i = 0; i <= array.length - 1; i++) {
if ((i + 1) % student == 0) {
array[i] = !array[i];
}
}
if (debug) {
printArray(array);
}
}
for (int i = 0; i < array.length; i++) {
if (array[i]) {
System.out.println("Locker " + (i + 1) + " is open.");
}
}
}
public static int menu() {
// Create scanner
Scanner input = new Scanner(System.in);
int userInput;
do {
System.out.print("Enter 0 to quit, and 1 to enter the number of students: ");
userInput = input.nextInt();
if (userInput == 1) {
System.out.print("Enter the number of students: ");
userInput = input.nextInt();
break;
}
} while (userInput != 0);
return userInput;
}
public static void main(String[] args) {
final int N = menu();
boolean[] lockers = new boolean[N];
// Execute solve and print
solveAndPrint(lockers, N);
}
}

Binary file not shown.