ArrayList time
This commit is contained in:
83
Semester 2/Exams/Exam1_CalebFontenot/Location.html
Normal file
83
Semester 2/Exams/Exam1_CalebFontenot/Location.html
Normal file
@@ -0,0 +1,83 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Location.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}
|
||||
.ST5 {font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST6 {font-family: monospace; font-style: italic}
|
||||
.ST3 {color: #ce54b8; font-family: monospace; font-style: italic}
|
||||
.ST0 {color: #287bde}
|
||||
.ST4 {color: #ce54b8}
|
||||
.string {color: #1e9347}
|
||||
.literal {color: #336bdd}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">C:\Users\ar114\Documents\NetBeansProjects\Exam1_CalebFontenot\src\main\java\com\calebfontenot\exam1_calebfontenot\Location.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/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> com.calebfontenot.exam1_calebfontenot;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">ar114</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> <span class="ST2">Location</span> {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">row</span>;
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST3">column</span>;
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">double</span> <span class="ST3">maxValue</span>;
|
||||
|
||||
<span class="literal">int</span> <span class="ST4">x</span>, <span class="ST4">y</span>;
|
||||
<span class="ST2">Location</span>(<span class="literal">int</span> x, <span class="literal">int</span> y) {
|
||||
<span class="literal">this</span>.<span class="ST4">x</span> = x;
|
||||
<span class="literal">this</span>.<span class="ST4">y</span> = y;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> Location <span class="ST5">locateLargest</span>(<span class="literal">double</span>[][] arr) {
|
||||
<span class="ST3">maxValue</span> = arr[0][0];
|
||||
<span class="comment">//Find max value</span>
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i < arr.<span class="ST4">length</span> - 1; ++i) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> j = 0; j < arr[i].<span class="ST4">length</span>; ++j) {
|
||||
<span class="literal">if</span> (<span class="ST3">maxValue</span> < arr[i][j]) {
|
||||
<span class="ST3">maxValue</span> = arr[i][j];
|
||||
<span class="ST3">column</span> = i;
|
||||
<span class="ST3">row</span> = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> <span class="literal">new</span> Location(<span class="ST3">c</span><span class="ST3">olumn</span>, <span class="ST3">r</span><span class="ST3">ow</span>);
|
||||
}
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> String <span class="ST2">toString</span>() {
|
||||
String s = <span class="string">"</span><span class="string">The location of the largest element is </span><span class="string">"</span>;
|
||||
s += <span class="ST3">maxValue</span> + <span class="string">"</span><span class="string"> at (</span><span class="string">"</span> + <span class="literal">this</span>.<span class="ST4">x</span> + <span class="string">"</span><span class="string">, </span><span class="string">"</span>+ <span class="literal">this</span>.<span class="ST4">y</span> +<span class="string">"</span><span class="string">)</span><span class="string">"</span>;
|
||||
<span class="literal">return</span> s;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args) {
|
||||
<span class="literal">double</span>[][] arr = {
|
||||
{23.5, 35, 2, 10, 12},
|
||||
{4.5, 3, 45},
|
||||
{35, 44, 5.5, 9.6}
|
||||
};
|
||||
System.<span class="ST3">out</span>.println(<span class="ST6">l</span><span class="ST6">ocateLargest</span>(arr));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
152
Semester 2/Exams/Exam1_CalebFontenot/MyStringBuilder.html
Normal file
152
Semester 2/Exams/Exam1_CalebFontenot/MyStringBuilder.html
Normal file
@@ -0,0 +1,152 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>MyStringBuilder.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}
|
||||
.ST5 {font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST4 {font-family: monospace; font-style: italic}
|
||||
.ST6 {color: #ce54b8; font-family: monospace; font-style: italic}
|
||||
.ST0 {color: #287bde}
|
||||
.ST3 {color: #ce54b8}
|
||||
.string {color: #1e9347}
|
||||
.literal {color: #336bdd}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">C:\Users\ar114\Documents\NetBeansProjects\Exam1_CalebFontenot\src\main\java\com\calebfontenot\exam1_calebfontenot\MyStringBuilder.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/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> com.calebfontenot.exam1_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.util.Arrays;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">ar114</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> <span class="ST2">MyStringBuilder</span> {
|
||||
|
||||
<span class="comment">//private int size = 0; // Size is not necessary. Remove it and modify the code.</span>
|
||||
<span class="comment">//private int capacity = 0; //not necessary</span>
|
||||
<span class="literal">private</span> <span class="literal">char</span>[] <span class="ST3">buffer</span>;
|
||||
|
||||
<span class="literal">public</span> <span class="ST2">MyStringBuilder</span>() {
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="ST2">MyStringBuilder</span>(<span class="literal">char</span>[] chars) {
|
||||
<span class="ST3">buffer</span> = chars;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="ST2">MyStringBuilder</span>(String s) {
|
||||
<span class="ST3">buffer</span> = s.toCharArray();
|
||||
}
|
||||
|
||||
<span class="literal">private</span> <span class="literal">void</span> <span class="ST2">increaseCapacity</span>(<span class="literal">int</span> newCapacity) {
|
||||
<span class="literal">char</span>[] temp = <span class="literal">new</span> <span class="literal">char</span>[newCapacity];
|
||||
System.<span class="ST4">arraycopy</span>(<span class="ST3">b</span><span class="ST3">uffer</span>, 0, temp, 0, <span class="ST3">b</span><span class="ST3">uffer</span>.<span class="ST3">length</span>);
|
||||
<span class="ST3">buffer</span> = temp;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> MyStringBuilder <span class="ST2">append</span>(MyStringBuilder s) {
|
||||
<span class="literal">int</span> oldLength = <span class="ST3">buffer</span>.<span class="ST3">length</span>;
|
||||
<span class="literal">char</span>[] toAdd = s.<span class="ST3">buffer</span>;
|
||||
increaseCapacity(toAdd.<span class="ST3">length</span> + oldLength);
|
||||
<span class="literal">int</span> j = 0;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = oldLength; i < (oldLength + s.<span class="ST3">buffer</span>.<span class="ST3">length</span>); ++i) {
|
||||
<span class="ST3">buffer</span>[i] = toAdd[j++];
|
||||
}
|
||||
<span class="literal">return</span> <span class="literal">this</span>;
|
||||
}
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="ST1">public</span> <span class="ST1">MyStringBuilder</span> <span class="ST1">append</span><span class="ST1">(</span><span class="ST1">int</span> <span class="ST1">i</span><span class="comment">) {</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * }</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@return</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">length</span>() {
|
||||
<span class="literal">return</span> <span class="ST3">buffer</span>.<span class="ST3">length</span> -1;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">char</span> <span class="ST2">charAt</span>(<span class="literal">int</span> index) {
|
||||
<span class="literal">return</span> <span class="ST3">buffer</span>[index];
|
||||
}
|
||||
|
||||
<span class="literal">public</span> MyStringBuilder <span class="ST2">toLowerCase</span>() {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i < <span class="ST3">buffer</span>.<span class="ST3">length</span>; ++i) {
|
||||
<span class="ST3">buffer</span>[i] = Character.<span class="ST4">toLowerCase</span>(<span class="ST3">buffer</span>[i]);
|
||||
}
|
||||
<span class="literal">return</span> <span class="literal">this</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> MyStringBuilder <span class="ST2">substring</span>(<span class="literal">int</span> begin, <span class="literal">int</span> end) {
|
||||
String s = <span class="string">""</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = begin; i < end; ++i) {
|
||||
s += <span class="ST3">buffer</span>[i];
|
||||
}
|
||||
<span class="literal">return</span> <span class="literal">new</span> MyStringBuilder(s.toCharArray());
|
||||
}
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> String <span class="ST2">toString</span>() {
|
||||
String s = <span class="string">""</span>;
|
||||
<span class="literal">for</span>(<span class="literal">char</span> c: <span class="ST3">buffer</span>)
|
||||
{
|
||||
s += c;
|
||||
}
|
||||
<span class="literal">return</span> s;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> MyStringBuilder <span class="ST2">reverse</span>() {
|
||||
<span class="literal">char</span>[] reversed = <span class="literal">new</span> <span class="literal">char</span>[<span class="ST3">buffer</span>.<span class="ST3">length</span>];
|
||||
<span class="literal">int</span> j = 0;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="ST3">buffer</span>.<span class="ST3">length</span> - 1; i >= 0; --i) {
|
||||
reversed[i] = <span class="ST3">buffer</span>[j];
|
||||
j++;
|
||||
}
|
||||
<span class="ST3">buffer</span> = reversed;
|
||||
<span class="literal">return</span> <span class="literal">this</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> MyStringBuilder <span class="ST2">substring</span>(<span class="literal">int</span> begin) {
|
||||
<span class="literal">return</span> substring(begin, <span class="ST3">b</span><span class="ST3">uffer</span>.<span class="ST3">length</span>);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> MyStringBuilder <span class="ST2">toUpperCase</span>() {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i < <span class="ST3">buffer</span>.<span class="ST3">length</span>; ++i) {
|
||||
<span class="ST3">buffer</span>[i] = Character.<span class="ST4">toUpperCase</span>(<span class="ST3">buffer</span>[i]);
|
||||
}
|
||||
<span class="literal">return</span> <span class="literal">this</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args) {
|
||||
MyStringBuilder s1 = <span class="literal">new</span> MyStringBuilder(<span class="string">"</span><span class="string">Welcomne to</span><span class="string">"</span>);
|
||||
MyStringBuilder s2 = <span class="literal">new</span> MyStringBuilder(<span class="string">"</span><span class="string"> Java</span><span class="string">"</span>);
|
||||
|
||||
System.<span class="ST6">out</span>.println(s1.length());
|
||||
System.<span class="ST6">out</span>.println(s1.charAt(3));
|
||||
System.<span class="ST6">out</span>.println(s1.toUpperCase().toString());
|
||||
s1.append(s2);
|
||||
System.<span class="ST6">out</span>.println(s1.toString());
|
||||
System.<span class="ST6">out</span>.println(s1.substring(1, 4));
|
||||
System.<span class="ST6">out</span>.println(s1.reverse());
|
||||
System.<span class="ST6">out</span>.println(s1.reverse());
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
82
Semester 2/Exams/Exam1_CalebFontenot/Question1.html
Normal file
82
Semester 2/Exams/Exam1_CalebFontenot/Question1.html
Normal file
@@ -0,0 +1,82 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Question1.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}
|
||||
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST6 {font-family: monospace; font-style: italic}
|
||||
.ST5 {color: #ce54b8; font-family: monospace; font-style: italic}
|
||||
.ST0 {color: #287bde}
|
||||
.ST4 {color: #ce54b8}
|
||||
.string {color: #1e9347}
|
||||
.literal {color: #336bdd}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">C:\Users\ar114\Documents\NetBeansProjects\Exam1_CalebFontenot\src\main\java\com\calebfontenot\exam1_calebfontenot\Question1.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/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> com.calebfontenot.exam1_calebfontenot;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">ar114</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> <span class="ST2">Question1</span> {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">sort</span>(<span class="literal">int</span>[] arr) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i < arr.<span class="ST4">length</span> - 1; ++i) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> j = i + 1; j < arr.<span class="ST4">length</span>; ++j) {
|
||||
<span class="literal">if</span> (arr[i] > arr[j]) {
|
||||
<span class="literal">int</span> temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">print</span>(<span class="literal">int</span>[][][] arr) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i < arr.<span class="ST4">length</span>; ++i) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> j = 0; j < arr[i].<span class="ST4">length</span>; ++j) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> k = 0; k < arr[i][j].<span class="ST4">length</span>; ++k) {
|
||||
System.<span class="ST5">out</span>.print(arr[i][j][k] + <span class="string">"</span> <span class="string">"</span>);
|
||||
}
|
||||
System.<span class="ST5">out</span>.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">main</span>(String[] args) {
|
||||
<span class="literal">int</span>[][][] arr = {
|
||||
{
|
||||
{11, 2, 6, 22}, {3, 90, 112, 40, 7, 12}
|
||||
},
|
||||
{
|
||||
{10, -20, 3}, {300, 50}
|
||||
}
|
||||
|
||||
};
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i < arr.<span class="ST4">length</span>; ++i) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> j = 0; j < arr[i].<span class="ST4">length</span>; ++j) {
|
||||
<span class="ST6">sort</span>(arr[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
<span class="ST6">print</span>(arr);
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
14
Semester 2/Exams/Exam1_CalebFontenot/pom.xml
Normal file
14
Semester 2/Exams/Exam1_CalebFontenot/pom.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.calebfontenot.exam1_calebfontenot</groupId>
|
||||
<artifactId>Exam1_CalebFontenot</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<exec.mainClass>com.calebfontenot.exam1_calebfontenot.Exam1_CalebFontenot</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* 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.exam1_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ar114
|
||||
*/
|
||||
public class Exam1_CalebFontenot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!".toUpperCase());
|
||||
}
|
||||
}
|
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.calebfontenot.exam1_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ar114
|
||||
*/
|
||||
public class Location {
|
||||
|
||||
public static int row;
|
||||
public static int column;
|
||||
public static double maxValue;
|
||||
|
||||
int x, y;
|
||||
Location(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public static Location locateLargest(double[][] arr) {
|
||||
maxValue = arr[0][0];
|
||||
//Find max value
|
||||
for (int i = 0; i < arr.length - 1; ++i) {
|
||||
for (int j = 0; j < arr[i].length; ++j) {
|
||||
if (maxValue < arr[i][j]) {
|
||||
maxValue = arr[i][j];
|
||||
column = i;
|
||||
row = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Location(column, row);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = "The location of the largest element is ";
|
||||
s += maxValue + " at (" + this.x + ", "+ this.y +")";
|
||||
return s;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
double[][] arr = {
|
||||
{23.5, 35, 2, 10, 12},
|
||||
{4.5, 3, 45},
|
||||
{35, 44, 5.5, 9.6}
|
||||
};
|
||||
System.out.println(locateLargest(arr));
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,123 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.calebfontenot.exam1_calebfontenot;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ar114
|
||||
*/
|
||||
public class MyStringBuilder {
|
||||
|
||||
//private int size = 0; // Size is not necessary. Remove it and modify the code.
|
||||
//private int capacity = 0; //not necessary
|
||||
private char[] buffer;
|
||||
|
||||
public MyStringBuilder() {
|
||||
}
|
||||
|
||||
public MyStringBuilder(char[] chars) {
|
||||
buffer = chars;
|
||||
}
|
||||
|
||||
public MyStringBuilder(String s) {
|
||||
buffer = s.toCharArray();
|
||||
}
|
||||
|
||||
private void increaseCapacity(int newCapacity) {
|
||||
char[] temp = new char[newCapacity];
|
||||
System.arraycopy(buffer, 0, temp, 0, buffer.length);
|
||||
buffer = temp;
|
||||
}
|
||||
|
||||
public MyStringBuilder append(MyStringBuilder s) {
|
||||
int oldLength = buffer.length;
|
||||
char[] toAdd = s.buffer;
|
||||
increaseCapacity(toAdd.length + oldLength);
|
||||
int j = 0;
|
||||
for (int i = oldLength; i < (oldLength + s.buffer.length); ++i) {
|
||||
buffer[i] = toAdd[j++];
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* public MyStringBuilder append(int i) {
|
||||
*
|
||||
* }
|
||||
*
|
||||
* @return
|
||||
*
|
||||
*/
|
||||
public int length() {
|
||||
return buffer.length -1;
|
||||
}
|
||||
|
||||
public char charAt(int index) {
|
||||
return buffer[index];
|
||||
}
|
||||
|
||||
public MyStringBuilder toLowerCase() {
|
||||
for (int i = 0; i < buffer.length; ++i) {
|
||||
buffer[i] = Character.toLowerCase(buffer[i]);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public MyStringBuilder substring(int begin, int end) {
|
||||
String s = "";
|
||||
for (int i = begin; i < end; ++i) {
|
||||
s += buffer[i];
|
||||
}
|
||||
return new MyStringBuilder(s.toCharArray());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = "";
|
||||
for(char c: buffer)
|
||||
{
|
||||
s += c;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public MyStringBuilder reverse() {
|
||||
char[] reversed = new char[buffer.length];
|
||||
int j = 0;
|
||||
for (int i = buffer.length - 1; i >= 0; --i) {
|
||||
reversed[i] = buffer[j];
|
||||
j++;
|
||||
}
|
||||
buffer = reversed;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MyStringBuilder substring(int begin) {
|
||||
return substring(begin, buffer.length);
|
||||
}
|
||||
|
||||
public MyStringBuilder toUpperCase() {
|
||||
for (int i = 0; i < buffer.length; ++i) {
|
||||
buffer[i] = Character.toUpperCase(buffer[i]);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
MyStringBuilder s1 = new MyStringBuilder("Welcome to");
|
||||
MyStringBuilder s2 = new MyStringBuilder(" Java");
|
||||
|
||||
System.out.println(s1.length());
|
||||
System.out.println(s1.charAt(3));
|
||||
System.out.println(s1.toUpperCase().toString());
|
||||
s1.append(s2);
|
||||
System.out.println(s1.toString());
|
||||
System.out.println(s1.substring(1, 4));
|
||||
System.out.println(s1.reverse());
|
||||
System.out.println(s1.reverse());
|
||||
}
|
||||
}
|
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.calebfontenot.exam1_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ar114
|
||||
*/
|
||||
public class Question1 {
|
||||
|
||||
public static void sort(int[] arr) {
|
||||
for (int i = 0; i < arr.length - 1; ++i) {
|
||||
for (int j = i + 1; j < arr.length; ++j) {
|
||||
if (arr[i] > arr[j]) {
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void print(int[][][] arr) {
|
||||
for (int i = 0; i < arr.length; ++i) {
|
||||
for (int j = 0; j < arr[i].length; ++j) {
|
||||
for (int k = 0; k < arr[i][j].length; ++k) {
|
||||
System.out.print(arr[i][j][k] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
int[][][] arr = {
|
||||
{
|
||||
{11, 2, 6, 22}, {3, 90, 112, 40, 7, 12}
|
||||
},
|
||||
{
|
||||
{10, -20, 3}, {300, 50}
|
||||
}
|
||||
|
||||
};
|
||||
for (int i = 0; i < arr.length; ++i) {
|
||||
for (int j = 0; j < arr[i].length; ++j) {
|
||||
sort(arr[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
print(arr);
|
||||
}
|
||||
}
|
111
Semester 2/Exams/PracticeExam1/Person.html
Normal file
111
Semester 2/Exams/PracticeExam1/Person.html
Normal file
@@ -0,0 +1,111 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Person.java</title>
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||
<style type="text/css">
|
||||
<!--
|
||||
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.ST2 {color: #9876aa}
|
||||
.ST3 {color: #ffc66d}
|
||||
.string {color: #6a8759}
|
||||
.number {color: #6897bb}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST5 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||||
.ST6 {color: #9876aa; font-family: monospace; font-style: italic}
|
||||
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.ST0 {color: #287bde}
|
||||
.literal {color: #cc7832}
|
||||
.ST4 {font-family: monospace; font-style: italic}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Exams/PracticeExam1/src/main/java/com/calebfontenot/practiceexam1/Person.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/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> com.calebfontenot.practiceexam1;
|
||||
|
||||
<span class="literal">import</span> java.util.GregorianCalendar;
|
||||
<span class="literal">import</span> java.util.Objects;
|
||||
|
||||
<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> Person {
|
||||
<span class="literal">private</span> String <span class="ST2">name</span>;
|
||||
<span class="literal">private</span> <span class="literal">final</span> GregorianCalendar <span class="ST2">birthDate</span>;
|
||||
|
||||
<span class="literal">public</span> Person(String name, GregorianCalendar brithDate)
|
||||
{
|
||||
<span class="literal">this</span>.<span class="ST2">name</span> = name;
|
||||
<span class="literal">this</span>.<span class="ST2">birthDate</span> = brithDate;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> String <span class="ST3">getName</span>()
|
||||
{
|
||||
<span class="literal">return</span> <span class="ST2">name</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">setName</span>(String name)
|
||||
{
|
||||
<span class="literal">this</span>.<span class="ST2">name</span> = name;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> GregorianCalendar <span class="ST3">getBirthdate</span>() {
|
||||
<span class="literal">return</span> <span class="literal">this</span>.<span class="ST2">birthDate</span>;
|
||||
}
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">equals</span>(Object obj)
|
||||
{
|
||||
<span class="literal">if</span> (<span class="literal">this</span> == obj) {
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
<span class="literal">if</span> (obj == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
<span class="literal">if</span> (getClass() != obj.getClass()) {
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
<span class="literal">final</span> Person other = (Person) obj;
|
||||
<span class="literal">if</span> (!Objects.<span class="ST4">equals</span>(<span class="literal">t</span><span class="literal">his</span>.<span class="ST2">name</span>, other.<span class="ST2">name</span>)) {
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
<span class="literal">return</span> Objects.<span class="ST4">equals</span>(<span class="literal">t</span><span class="literal">his</span>.<span class="ST2">birthDate</span>, other.<span class="ST2">birthDate</span>);
|
||||
}
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> String <span class="ST3">toString</span>()
|
||||
{
|
||||
<span class="literal">return</span> <span class="string">"</span><span class="string">Person{</span><span class="string">"</span> + <span class="string">"</span><span class="string">name=</span><span class="string">"</span> + <span class="ST2">name</span> + <span class="string">"</span><span class="string">, birthDate=</span><span class="string">"</span> + <span class="ST2">birthDate</span>.toInstant() + <span class="string">'</span><span class="string">}</span><span class="string">'</span>;
|
||||
}
|
||||
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args)
|
||||
{
|
||||
Person john = <span class="literal">new</span> Person(<span class="string">"</span><span class="string">John</span><span class="string">"</span>, <span class="literal">new</span> GregorianCalendar(<span class="number">2</span><span class="number">003</span>, <span class="number">8</span>, <span class="number">2</span><span class="number">8</span>));
|
||||
Person mary = <span class="literal">new</span> Person(<span class="string">"</span><span class="string">Mary</span><span class="string">"</span>, <span class="literal">new</span> GregorianCalendar(<span class="number">2</span><span class="number">004</span>, <span class="number">3</span>, <span class="number">2</span><span class="number">3</span>));
|
||||
System.<span class="ST6">out</span>.println(john);
|
||||
System.<span class="ST6">out</span>.println(mary);
|
||||
Person john1 = <span class="literal">new</span> Person(<span class="string">"</span><span class="string">John</span><span class="string">"</span>, <span class="literal">new</span> GregorianCalendar(<span class="number">2</span><span class="number">003</span>, <span class="number">8</span>, <span class="number">2</span><span class="number">8</span>));
|
||||
Person john2 = <span class="literal">new</span> Person(<span class="string">"</span><span class="string">John</span><span class="string">"</span>, <span class="literal">new</span> GregorianCalendar(<span class="number">2</span><span class="number">003</span>, <span class="number">1</span><span class="number">0</span>, <span class="number">1</span><span class="number">0</span>));
|
||||
Person john3 = <span class="literal">new</span> Person(<span class="string">"</span><span class="string">John</span><span class="string">"</span>, <span class="literal">new</span> GregorianCalendar(<span class="number">2</span><span class="number">003</span>, <span class="number">8</span>, <span class="number">2</span><span class="number">8</span>));
|
||||
|
||||
System.<span class="ST6">out</span>.println(john1.equals(john));
|
||||
System.<span class="ST6">out</span>.println(john2.equals(john));
|
||||
System.<span class="ST6">out</span>.println(john3.equals(john));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
</pre></body>
|
||||
</html>
|
174
Semester 2/Exams/PracticeExam1/Problem1.html
Normal file
174
Semester 2/Exams/PracticeExam1/Problem1.html
Normal file
@@ -0,0 +1,174 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Problem1ArrayNames.java</title>
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||
<style type="text/css">
|
||||
<!--
|
||||
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.ST3 {color: #9876aa}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||||
.ST4 {color: #9876aa; font-family: monospace; font-style: italic}
|
||||
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.ST5 {color: #8a653b}
|
||||
.ST0 {color: #287bde}
|
||||
.literal {color: #cc7832}
|
||||
.ST6 {font-family: monospace; font-style: italic}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Exams/PracticeExam1/src/main/java/com/calebfontenot/practiceexam1/Problem1ArrayNames.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/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> com.calebfontenot.practiceexam1;
|
||||
|
||||
<span class="literal">import</span> java.util.Arrays;
|
||||
<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> Problem1ArrayNames {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST2">maxNumberOfColumnsInJagged2dArray</span>(<span class="literal">char</span>[][] ar)
|
||||
{
|
||||
<span class="literal">int</span> maxNumberOfColumns = <span class="number">0</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> row = <span class="number">0</span>; row < ar.<span class="ST3">length</span>; ++row) {
|
||||
<span class="literal">if</span> (ar[row].<span class="ST3">length</span> > maxNumberOfColumns) {
|
||||
maxNumberOfColumns = ar[row].<span class="ST3">length</span>;
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> maxNumberOfColumns;
|
||||
}
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="comment">Displays</span> <span class="comment">Enter</span> <span class="comment">number</span> <span class="comment">of</span> <span class="comment">names</span> <span class="comment">or</span><span class="comment"> -1 </span><span class="comment">to</span> <span class="comment">quit</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">number</span> <span class="comment">of</span> <span class="comment">students</span> <span class="comment">or</span><span class="comment"> -1;</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST2">menu</span>()
|
||||
{
|
||||
<span class="literal">int</span> userInt = -<span class="number">1</span>;
|
||||
<span class="literal">do</span> {
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Enter the numbrer of students or -1 to quit: </span><span class="string">"</span>);
|
||||
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST4">in</span>);
|
||||
userInt = input.nextInt();
|
||||
<span class="literal">if</span> (userInt == <span class="number">0</span>) {
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Input cannot be 0!</span><span class="string">"</span>);
|
||||
} <span class="literal">else</span> {
|
||||
<span class="literal">return</span> userInt;
|
||||
}
|
||||
} <span class="literal">while</span> (userInt != -<span class="number">1</span>);
|
||||
<span class="literal">return</span> -<span class="number">1</span>;
|
||||
}
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="comment">Reads</span> <span class="comment">form</span> <span class="comment">user</span> <span class="comment">a</span> <span class="comment">number</span> <span class="comment">of</span> <span class="comment">names</span><span class="comment">, </span><span class="comment">creates</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">of</span> <span class="comment">names</span> <span class="comment">and</span> <span class="comment">returns</span> <span class="comment">it</span><span class="comment">.</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST5">numberOfNames</span>
|
||||
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">array</span> <span class="comment">of</span> <span class="comment">names</span><span class="comment">.</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">char</span>[][] <span class="ST2">readNames</span>(<span class="literal">int</span> numOfNames)
|
||||
{
|
||||
<span class="literal">char</span>[][] names = <span class="literal">new</span> <span class="literal">char</span>[numOfNames][];
|
||||
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST4">in</span>);
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < numOfNames; i++) {
|
||||
System.<span class="ST4">out</span>.print(<span class="string">"</span><span class="string">Enter a name: </span><span class="string">"</span>);
|
||||
names[i] = input.next().toCharArray();
|
||||
}
|
||||
<span class="literal">return</span> names;
|
||||
}
|
||||
<span class="literal">public</span> <span class="literal">static</span> String <span class="ST2">returnRowMajorOrder</span>(<span class="literal">char</span>[] ar)<span class="comment">//normal</span>
|
||||
{
|
||||
String returnString = <span class="string">""</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> x = <span class="number">0</span>; x < ar.<span class="ST3">length</span>; ++x) {
|
||||
returnString += ar[x];
|
||||
}
|
||||
<span class="literal">return</span> returnString;
|
||||
}
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="comment">Sorts</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">in</span> <span class="comment">ascending</span> <span class="comment">order</span><span class="comment">.</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST5">ar</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">sortNames</span>(<span class="literal">char</span>[][] names)
|
||||
{
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < names.<span class="ST3">length</span> - <span class="number">1</span>; ++i) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> j = i + <span class="number">1</span>; j < names.<span class="ST3">length</span>; ++j) {
|
||||
<span class="literal">char</span> compChar1 = names[i][<span class="number">0</span>], compChar2 = names[j][<span class="number">0</span>];
|
||||
<span class="comment">// Reoder entire row</span>
|
||||
<span class="literal">for</span> (<span class="literal">int</span> rowIterate = <span class="number">1</span>; rowIterate < <span class="ST6">maxNumberOfColumnsInJagged2dArray</span>(names); ++rowIterate) {
|
||||
<span class="literal">if</span> (Character.<span class="ST6">toLowerCase</span>(compChar1) == Character.<span class="ST6">toLowerCase</span>(compChar2)) {
|
||||
<span class="literal">try</span> {
|
||||
compChar1 = names[i][rowIterate];
|
||||
compChar2 = names[j][rowIterate];
|
||||
} <span class="literal">catch</span> (Exception ex) {
|
||||
<span class="comment">// If it's failed, the index has gone out of range.</span>
|
||||
<span class="comment">// Check the length of the arrays and swap the larger one with the smaller one.</span>
|
||||
<span class="literal">if</span> (names[i].<span class="ST3">length</span> > names[j].<span class="ST3">length</span>) {
|
||||
System.<span class="ST4">out</span>.println(names[i].<span class="ST3">length</span> + <span class="string">"</span> <span class="string">"</span> + names[j].<span class="ST3">length</span>);
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Swapping </span><span class="string">"</span> + <span class="ST6">returnRowMajorOrder</span>(names[i]) + <span class="string">"</span><span class="string"> with </span><span class="string">"</span> + <span class="ST6">returnRowMajorOrder</span>(names[j]));
|
||||
<span class="literal">char</span>[] temp = names[i];
|
||||
names[i] = names[j];
|
||||
names[j] = temp;
|
||||
}
|
||||
<span class="literal">break</span>;
|
||||
}
|
||||
|
||||
}
|
||||
<span class="literal">if</span> (Character.<span class="ST6">toLowerCase</span>(compChar1) > Character.<span class="ST6">toLowerCase</span>(compChar2)) {
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Swapping </span><span class="string">"</span> + <span class="ST6">returnRowMajorOrder</span>(names[i]) + <span class="string">"</span><span class="string"> with </span><span class="string">"</span> + <span class="ST6">returnRowMajorOrder</span>(names[j]));
|
||||
<span class="literal">char</span>[] temp = names[i];
|
||||
names[i] = names[j];
|
||||
names[j] = temp;
|
||||
<span class="literal">break</span>;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="comment">Prints</span> <span class="comment">the</span> <span class="comment">array</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST5">ar</span> <span class="comment">is</span> <span class="comment">printed</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">print</span>(<span class="literal">char</span>[][] ar)
|
||||
{
|
||||
<span class="literal">for</span> (<span class="literal">char</span>[] nested : ar) {
|
||||
System.<span class="ST4">out</span>.println(Arrays.<span class="ST6">toString</span>(nested));
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args)
|
||||
{
|
||||
<span class="literal">int</span> numOfNames = <span class="ST6">menu</span>();
|
||||
<span class="literal">char</span>[][] names = <span class="literal">null</span>;
|
||||
<span class="literal">while</span> (numOfNames != -<span class="number">1</span>) {
|
||||
names = <span class="ST6">readNames</span>(numOfNames);
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Original Names:</span><span class="string">"</span>);
|
||||
<span class="ST6">print</span>(names);
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Sorted Names:</span><span class="string">"</span>);
|
||||
<span class="ST6">sortNames</span>(names);
|
||||
<span class="ST6">print</span>(names);
|
||||
numOfNames = <span class="ST6">menu</span>();
|
||||
}
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">goodbye!</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
169
Semester 2/Exams/PracticeExam1/Problem2.html
Normal file
169
Semester 2/Exams/PracticeExam1/Problem2.html
Normal file
@@ -0,0 +1,169 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>Problem2ArrayObjects.java</title>
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||
<style type="text/css">
|
||||
<!--
|
||||
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
|
||||
table {color: #888888; background-color: #313335; font-family: monospace}
|
||||
.ST3 {color: #9876aa}
|
||||
.number {color: #6897bb}
|
||||
.string {color: #6a8759}
|
||||
.comment {color: #808080}
|
||||
.whitespace {color: #505050}
|
||||
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
|
||||
.ST4 {color: #9876aa; font-family: monospace; font-style: italic}
|
||||
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
|
||||
.ST5 {color: #8a653b}
|
||||
.ST0 {color: #287bde}
|
||||
.literal {color: #cc7832}
|
||||
.ST6 {font-family: monospace; font-style: italic}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Exams/PracticeExam1/src/main/java/com/calebfontenot/practiceexam1/Problem2ArrayObjects.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/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> com.calebfontenot.practiceexam1;
|
||||
|
||||
<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> Problem2ArrayObjects {
|
||||
|
||||
<span class="literal">private</span> String[] <span class="comment">names</span>;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="comment">Displays</span><span class="comment">: </span><span class="comment">Enter</span> <span class="comment">number</span> <span class="comment">of</span> <span class="comment">names</span> <span class="comment">or</span><span class="comment"> -1 </span><span class="comment">to</span> <span class="comment">quit</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">number</span> <span class="comment">of</span> <span class="comment">students</span> <span class="comment">or</span><span class="comment"> -1;</span>
|
||||
<span class="comment">*/</span>
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST2">maxNumberOfColumnsInStringArray</span>(String[] ar)
|
||||
{
|
||||
<span class="literal">int</span> maxNumberOfColumns = <span class="number">0</span>;
|
||||
<span class="literal">for</span> (<span class="literal">int</span> row = <span class="number">0</span>; row < ar.<span class="ST3">length</span>; ++row) {
|
||||
<span class="literal">if</span> (ar[row].length() > maxNumberOfColumns) {
|
||||
maxNumberOfColumns = ar[row].length();
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> maxNumberOfColumns;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">int</span> <span class="ST2">menu</span>()
|
||||
{
|
||||
<span class="literal">int</span> userInt = -<span class="number">1</span>;
|
||||
<span class="literal">do</span> {
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Enter the numbrer of students or -1 to quit: </span><span class="string">"</span>);
|
||||
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST4">in</span>);
|
||||
userInt = input.nextInt();
|
||||
<span class="literal">if</span> (userInt == <span class="number">0</span>) {
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Input cannot be 0!</span><span class="string">"</span>);
|
||||
} <span class="literal">else</span> {
|
||||
<span class="literal">return</span> userInt;
|
||||
}
|
||||
} <span class="literal">while</span> (userInt != -<span class="number">1</span>);
|
||||
<span class="literal">return</span> -<span class="number">1</span>;
|
||||
}
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="comment">Reads</span> <span class="comment">form</span> <span class="comment">user</span> <span class="comment">a</span> <span class="comment">number</span> <span class="comment">of</span> <span class="comment">names</span><span class="comment">, </span><span class="comment">creates</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">of</span> <span class="comment">names</span> <span class="comment">and</span> <span class="comment">returns</span> <span class="comment">it</span><span class="comment">.</span> <span class="comment">Stores</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">into</span> <span class="comment">this</span><span class="comment">.</span><span class="comment">names</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST5">numberOfNames</span>
|
||||
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">array</span> <span class="comment">of</span> <span class="comment">names</span><span class="comment">.</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">static</span> String[] <span class="ST2">readNames</span>(<span class="literal">int</span> numOfNames)
|
||||
{
|
||||
String[] names = <span class="literal">new</span> String[numOfNames];
|
||||
Scanner input = <span class="literal">new</span> Scanner(System.<span class="ST4">in</span>);
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < numOfNames; i++) {
|
||||
System.<span class="ST4">out</span>.print(<span class="string">"</span><span class="string">Enter a name: </span><span class="string">"</span>);
|
||||
names[i] = input.next();
|
||||
}
|
||||
<span class="literal">return</span> names;
|
||||
}
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="comment">Sorts</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">of</span> <span class="comment">names</span> <span class="comment">in</span> <span class="comment">ascending</span> <span class="comment">order</span> <span class="comment">Does</span> <span class="comment">not</span> <span class="comment">replace</span> <span class="comment">the</span> <span class="comment">this</span><span class="comment">.</span><span class="comment">names</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST5">ar</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">to</span> <span class="comment">be</span> <span class="comment">sorted</span>
|
||||
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">returns</span> <span class="comment">the</span> <span class="comment">sorted</span> <span class="comment">array</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">sortNames</span>(String[] names)
|
||||
{
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < names.<span class="ST3">length</span> - <span class="number">1</span>; ++i) {
|
||||
<span class="literal">for</span> (<span class="literal">int</span> j = i + <span class="number">1</span>; j < names.<span class="ST3">length</span>; ++j) {
|
||||
<span class="literal">char</span> compChar1 = names[i].charAt(<span class="number">0</span>), compChar2 = names[j].charAt(<span class="number">0</span>);
|
||||
<span class="comment">// Reoder entire row</span>
|
||||
<span class="literal">for</span> (<span class="literal">int</span> rowIterate = <span class="number">1</span>; rowIterate < <span class="ST6">maxNumberOfColumnsInStringArray</span>(names); ++rowIterate) {
|
||||
<span class="literal">if</span> (Character.<span class="ST6">toLowerCase</span>(compChar1) == Character.<span class="ST6">toLowerCase</span>(compChar2)) {
|
||||
<span class="literal">try</span> {
|
||||
compChar1 = names[i].charAt(rowIterate);
|
||||
compChar2 = names[j].charAt(rowIterate);
|
||||
} <span class="literal">catch</span> (Exception ex) {
|
||||
<span class="comment">// If it's failed, the index has gone out of range.</span>
|
||||
<span class="comment">// Check the length of the arrays and swap the larger one with the smaller one.</span>
|
||||
<span class="literal">if</span> (names[i].length() > names[j].length()) {
|
||||
System.<span class="ST4">out</span>.println(names[i].length() + <span class="string">"</span> <span class="string">"</span> + names[j].length());
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Swapping </span><span class="string">"</span> + names[i] + <span class="string">"</span><span class="string"> with </span><span class="string">"</span> + names[j]);
|
||||
String temp = names[i];
|
||||
names[i] = names[j];
|
||||
names[j] = temp;
|
||||
}
|
||||
<span class="literal">break</span>;
|
||||
}
|
||||
|
||||
}
|
||||
<span class="literal">if</span> (Character.<span class="ST6">toLowerCase</span>(compChar1) > Character.<span class="ST6">toLowerCase</span>(compChar2)) {
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Swapping </span><span class="string">"</span> + names[i] + <span class="string">"</span><span class="string"> with </span><span class="string">"</span> + names[j]);
|
||||
String temp = names[i];
|
||||
names[i] = names[j];
|
||||
names[j] = temp;
|
||||
<span class="literal">break</span>;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="comment">Prints</span> <span class="comment">the</span> <span class="comment">array</span> <span class="comment">of</span> <span class="comment">objects</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@param</span> <span class="ST5">ar</span> <span class="comment">is</span> <span class="comment">printed</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">print</span>(String[] arr)
|
||||
{
|
||||
<span class="literal">for</span> (String string: arr) {
|
||||
System.<span class="ST4">out</span>.println(string);
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args)
|
||||
{
|
||||
<span class="literal">int</span> numOfNames = <span class="ST6">menu</span>();
|
||||
String[] names = <span class="literal">null</span>;
|
||||
<span class="literal">while</span> (numOfNames != -<span class="number">1</span>) {
|
||||
names = <span class="ST6">readNames</span>(numOfNames);
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Original Names:</span><span class="string">"</span>);
|
||||
<span class="ST6">print</span>(names);
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Sorted Names:</span><span class="string">"</span>);
|
||||
<span class="ST6">sortNames</span>(names);
|
||||
<span class="ST6">print</span>(names);
|
||||
numOfNames = <span class="ST6">menu</span>();
|
||||
}
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">goodbye!</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
</pre></body>
|
||||
</html>
|
14
Semester 2/Exams/PracticeExam1/pom.xml
Normal file
14
Semester 2/Exams/PracticeExam1/pom.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.calebfontenot</groupId>
|
||||
<artifactId>PracticeExam1</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<exec.mainClass>com.calebfontenot.practiceexam1.PracticeExam1</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.calebfontenot.practiceexam1;
|
||||
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Person {
|
||||
private String name;
|
||||
private final GregorianCalendar birthDate;
|
||||
|
||||
public Person(String name, GregorianCalendar brithDate)
|
||||
{
|
||||
this.name = name;
|
||||
this.birthDate = brithDate;
|
||||
}
|
||||
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public GregorianCalendar getBirthdate() {
|
||||
return this.birthDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass()) {
|
||||
return false;
|
||||
}
|
||||
final Person other = (Person) obj;
|
||||
if (!Objects.equals(this.name, other.name)) {
|
||||
return false;
|
||||
}
|
||||
return Objects.equals(this.birthDate, other.birthDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Person{" + "name=" + name + ", birthDate=" + birthDate.toInstant() + '}';
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
Person john = new Person("John", new GregorianCalendar(2003, 8, 28));
|
||||
Person mary = new Person("Mary", new GregorianCalendar(2004, 3, 23));
|
||||
System.out.println(john);
|
||||
System.out.println(mary);
|
||||
Person john1 = new Person("John", new GregorianCalendar(2003, 8, 28));
|
||||
Person john2 = new Person("John", new GregorianCalendar(2003, 10, 10));
|
||||
Person john3 = new Person("John", new GregorianCalendar(2003, 8, 28));
|
||||
|
||||
System.out.println(john1.equals(john));
|
||||
System.out.println(john2.equals(john));
|
||||
System.out.println(john3.equals(john));
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package com.calebfontenot.practiceexam1;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class PracticeExam1 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.calebfontenot.practiceexam1;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
|
||||
|
||||
public class Problem1ArrayNames {
|
||||
|
||||
public static int maxNumberOfColumnsInJagged2dArray(char[][] ar)
|
||||
{
|
||||
int maxNumberOfColumns = 0;
|
||||
for (int row = 0; row < ar.length; ++row) {
|
||||
if (ar[row].length > maxNumberOfColumns) {
|
||||
maxNumberOfColumns = ar[row].length;
|
||||
}
|
||||
}
|
||||
return maxNumberOfColumns;
|
||||
}
|
||||
/**
|
||||
* Displays Enter number of names or -1 to quit
|
||||
*
|
||||
* @return number of students or -1;
|
||||
*/
|
||||
public static int menu()
|
||||
{
|
||||
int userInt = -1;
|
||||
do {
|
||||
System.out.println("Enter the numbrer of students or -1 to quit: ");
|
||||
Scanner input = new Scanner(System.in);
|
||||
userInt = input.nextInt();
|
||||
if (userInt == 0) {
|
||||
System.out.println("Input cannot be 0!");
|
||||
} else {
|
||||
return userInt;
|
||||
}
|
||||
} while (userInt != -1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads form user a number of names, creates the array of names and returns it.
|
||||
*
|
||||
* @param numberOfNames
|
||||
* @return array of names.
|
||||
*/
|
||||
public static char[][] readNames(int numOfNames)
|
||||
{
|
||||
char[][] names = new char[numOfNames][];
|
||||
Scanner input = new Scanner(System.in);
|
||||
for (int i = 0; i < numOfNames; i++) {
|
||||
System.out.print("Enter a name: ");
|
||||
names[i] = input.next().toCharArray();
|
||||
}
|
||||
return names;
|
||||
}
|
||||
public static String returnRowMajorOrder(char[] ar)//normal
|
||||
{
|
||||
String returnString = "";
|
||||
for (int x = 0; x < ar.length; ++x) {
|
||||
returnString += ar[x];
|
||||
}
|
||||
return returnString;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array in ascending order.
|
||||
*
|
||||
* @param ar
|
||||
*/
|
||||
public static void sortNames(char[][] names)
|
||||
{
|
||||
for (int i = 0; i < names.length - 1; ++i) {
|
||||
for (int j = i + 1; j < names.length; ++j) {
|
||||
char compChar1 = names[i][0], compChar2 = names[j][0];
|
||||
// Reoder entire row
|
||||
for (int rowIterate = 1; rowIterate < maxNumberOfColumnsInJagged2dArray(names); ++rowIterate) {
|
||||
if (Character.toLowerCase(compChar1) == Character.toLowerCase(compChar2)) {
|
||||
try {
|
||||
compChar1 = names[i][rowIterate];
|
||||
compChar2 = names[j][rowIterate];
|
||||
} catch (Exception ex) {
|
||||
// If it's failed, the index has gone out of range.
|
||||
// Check the length of the arrays and swap the larger one with the smaller one.
|
||||
if (names[i].length > names[j].length) {
|
||||
System.out.println(names[i].length + " " + names[j].length);
|
||||
System.out.println("Swapping " + returnRowMajorOrder(names[i]) + " with " + returnRowMajorOrder(names[j]));
|
||||
char[] temp = names[i];
|
||||
names[i] = names[j];
|
||||
names[j] = temp;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) {
|
||||
System.out.println("Swapping " + returnRowMajorOrder(names[i]) + " with " + returnRowMajorOrder(names[j]));
|
||||
char[] temp = names[i];
|
||||
names[i] = names[j];
|
||||
names[j] = temp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints the array
|
||||
*
|
||||
* @param ar is printed
|
||||
*/
|
||||
public static void print(char[][] ar)
|
||||
{
|
||||
for (char[] nested : ar) {
|
||||
System.out.println(Arrays.toString(nested));
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int numOfNames = menu();
|
||||
char[][] names = null;
|
||||
while (numOfNames != -1) {
|
||||
names = readNames(numOfNames);
|
||||
System.out.println("Original Names:");
|
||||
print(names);
|
||||
System.out.println("Sorted Names:");
|
||||
sortNames(names);
|
||||
print(names);
|
||||
numOfNames = menu();
|
||||
}
|
||||
System.out.println("goodbye!");
|
||||
}
|
||||
}
|
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package com.calebfontenot.practiceexam1;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class Problem2ArrayObjects {
|
||||
|
||||
private String[] names;
|
||||
|
||||
/**
|
||||
* Displays: Enter number of names or -1 to quit
|
||||
*
|
||||
* @return number of students or -1;
|
||||
*/
|
||||
|
||||
public static int maxNumberOfColumnsInStringArray(String[] ar)
|
||||
{
|
||||
int maxNumberOfColumns = 0;
|
||||
for (int row = 0; row < ar.length; ++row) {
|
||||
if (ar[row].length() > maxNumberOfColumns) {
|
||||
maxNumberOfColumns = ar[row].length();
|
||||
}
|
||||
}
|
||||
return maxNumberOfColumns;
|
||||
}
|
||||
|
||||
public static int menu()
|
||||
{
|
||||
int userInt = -1;
|
||||
do {
|
||||
System.out.println("Enter the numbrer of students or -1 to quit: ");
|
||||
Scanner input = new Scanner(System.in);
|
||||
userInt = input.nextInt();
|
||||
if (userInt == 0) {
|
||||
System.out.println("Input cannot be 0!");
|
||||
} else {
|
||||
return userInt;
|
||||
}
|
||||
} while (userInt != -1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads form user a number of names, creates the array of names and returns it. Stores the array into this.names
|
||||
*
|
||||
* @param numberOfNames
|
||||
* @return array of names.
|
||||
*/
|
||||
public static String[] readNames(int numOfNames)
|
||||
{
|
||||
String[] names = new String[numOfNames];
|
||||
Scanner input = new Scanner(System.in);
|
||||
for (int i = 0; i < numOfNames; i++) {
|
||||
System.out.print("Enter a name: ");
|
||||
names[i] = input.next();
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts the array of names in ascending order Does not replace the this.names
|
||||
*
|
||||
* @param ar the array to be sorted
|
||||
* @return returns the sorted array
|
||||
*/
|
||||
public static void sortNames(String[] names)
|
||||
{
|
||||
for (int i = 0; i < names.length - 1; ++i) {
|
||||
for (int j = i + 1; j < names.length; ++j) {
|
||||
char compChar1 = names[i].charAt(0), compChar2 = names[j].charAt(0);
|
||||
// Reoder entire row
|
||||
for (int rowIterate = 1; rowIterate < maxNumberOfColumnsInStringArray(names); ++rowIterate) {
|
||||
if (Character.toLowerCase(compChar1) == Character.toLowerCase(compChar2)) {
|
||||
try {
|
||||
compChar1 = names[i].charAt(rowIterate);
|
||||
compChar2 = names[j].charAt(rowIterate);
|
||||
} catch (Exception ex) {
|
||||
// If it's failed, the index has gone out of range.
|
||||
// Check the length of the arrays and swap the larger one with the smaller one.
|
||||
if (names[i].length() > names[j].length()) {
|
||||
System.out.println(names[i].length() + " " + names[j].length());
|
||||
System.out.println("Swapping " + names[i] + " with " + names[j]);
|
||||
String temp = names[i];
|
||||
names[i] = names[j];
|
||||
names[j] = temp;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
if (Character.toLowerCase(compChar1) > Character.toLowerCase(compChar2)) {
|
||||
System.out.println("Swapping " + names[i] + " with " + names[j]);
|
||||
String temp = names[i];
|
||||
names[i] = names[j];
|
||||
names[j] = temp;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints the array of objects
|
||||
*
|
||||
* @param ar is printed
|
||||
*/
|
||||
public static void print(String[] arr)
|
||||
{
|
||||
for (String string: arr) {
|
||||
System.out.println(string);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
int numOfNames = menu();
|
||||
String[] names = null;
|
||||
while (numOfNames != -1) {
|
||||
names = readNames(numOfNames);
|
||||
System.out.println("Original Names:");
|
||||
print(names);
|
||||
System.out.println("Sorted Names:");
|
||||
sortNames(names);
|
||||
print(names);
|
||||
numOfNames = menu();
|
||||
}
|
||||
System.out.println("goodbye!");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user