114 lines
7.3 KiB
HTML
114 lines
7.3 KiB
HTML
|
<!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">"</span><span class="ST5">\u001B</span><span class="string">[31m</span><span class="string">"</span>;
|
||
|
<span class="literal">final</span> String ANSI_GREEN = <span class="string">"</span><span class="ST5">\u001B</span><span class="string">[32m</span><span class="string">"</span>;
|
||
|
<span class="literal">final</span> String ANSI_WHITE = <span class="string">"</span><span class="ST5">\u001B</span><span class="string">[37m</span><span class="string">"</span>;
|
||
|
|
||
|
<span class="comment">// Print the array</span>
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i <= 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">"</span> <span class="string">"</span>);
|
||
|
} <span class="literal">else</span> {
|
||
|
System.<span class="ST3">out</span>.print(ANSI_RED + array[i] + <span class="string">"</span> <span class="string">"</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 <= studentCount; student++) {
|
||
|
<span class="literal">if</span> (<span class="ST3">debug</span>) {
|
||
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Student </span><span class="string">"</span> + student + <span class="string">"</span><span class="string"> is interacting with the lockers!</span><span class="string">"</span>);
|
||
|
}
|
||
|
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i <= 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 < array.<span class="ST6">length</span>; i++) {
|
||
|
<span class="literal">if</span> (array[i]) {
|
||
|
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Locker </span><span class="string">"</span> + (i + 1) + <span class="string">"</span><span class="string"> is open.</span><span class="string">"</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">"</span><span class="string">Enter 0 to quit, and 1 to enter the number of students: </span><span class="string">"</span>);
|
||
|
userInput = input.nextInt();
|
||
|
<span class="literal">if</span> (userInput == 1) {
|
||
|
System.<span class="ST3">out</span>.print(<span class="string">"</span><span class="string">Enter the number of students: </span><span class="string">"</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>
|