Update: 2021-Jan-20

This commit is contained in:
Logen Kain 2021-01-20 06:57:22 -05:00
parent e820f29ed8
commit 1c6d704b0c
33 changed files with 208 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.swp
*.class

Binary file not shown.

View File

@ -0,0 +1,44 @@
import java.util.Scanner;
public class txt_msg_expander {
public static String replaceText(String input,
String replaceThis,
String withThis) {
System.out.println("Replaced \"" +replaceThis + "\" with \"" +
withThis + "\"");
return input.replace(replaceThis, withThis);
}
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Enter text:");
String usr_input = scnr.nextLine();
System.out.println("You entered: " + usr_input+"\n");
if (usr_input.contains("BFF")){
usr_input = replaceText(usr_input, "BFF", "best friend forever");
}
if (usr_input.contains("IDK")){
usr_input = replaceText(usr_input, "IDK", "I don't know");
}
if (usr_input.contains("JK")){
usr_input = replaceText(usr_input, "JK", "just kidding");
}
if (usr_input.contains("TMI")){
usr_input = replaceText(usr_input, "TMI", "too much information");
}
if (usr_input.contains("TTYL")){
usr_input = replaceText(usr_input, "TTYL", "talk to you later");
}
System.out.println("\nExpanded: " + usr_input);
scnr.close();
}
}

View File

@ -0,0 +1,16 @@
public class Widget {
private int inStock;
public Widget() {
inStock = 10;
}
public void addInventory(int amt) {
inStock = inStock + amt;
}
public static void main(String [] args){
Widget blueWidget = new Widget();
Widget greenWidget = new Widget();
blueWidget.addInventory(15);
greenWidget.addInventory(5);
}
}

20
old/java1/exam/wut.java Normal file
View File

@ -0,0 +1,20 @@
public class Student {
private double myGPA;
private int myCredits;
public void increaseCredits(int amount) {
myCredits = myCredits + amount;
}
public void setCredits(int credits) {
myCredits = credits;
}
public int getCredits() {
return myCredits;
}
public static void main(String [] args) {
Student s = new Student();
s.setCredits(6);
s.increaseCredits(12);
System.out.println(s.getCredits());
}
}

View File

@ -0,0 +1,27 @@
import java.util.Scanner;
import java.util.InputMismatchException;
public class NameAgeChecker {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String inputName;
int age;
inputName = scnr.next();
while (!inputName.equals("-1")) {
// FIXME: The following line will throw an InputMismatchException.
// Insert a try/catch statement to catch the exception.
try {
age = scnr.nextInt();
}
catch (InputMismatchException e){
age = -1;
scnr.nextLine();
}
System.out.println(inputName + " " + (age + 1));
inputName = scnr.next();
}
}
}

View File

@ -0,0 +1,29 @@
import java.util.Scanner;
public class painting_a_wall {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
double height, width;
final double square_feet_per_gallon = 350;
System.out.println("Enter wall height (feet):");
height = scnr.nextDouble();
System.out.println("Enter wall width (feet):");
width = scnr.nextDouble();
System.out.println("Wall area: " + height*width +
" square feet");
System.out.println("Paint needed: " +
(height*width)/square_feet_per_gallon +
" gallons");
System.out.println("Cans needed: " +
(int)Math.ceil((height*width)/square_feet_per_gallon)+
" can(s)");
scnr.close();
}
}

View File

@ -0,0 +1,26 @@
import java.util.Scanner;
import java.lang.StringBuffer;
public class palindrome {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Enter text:");
String usrInput = scnr.nextLine();
System.out.println();
String reverseUsrInput;
usrInput = usrInput.replace(" ", "");
reverseUsrInput = new StringBuffer(usrInput).reverse().toString();
if (usrInput.equals(reverseUsrInput)){
System.out.println(usrInput + " is a palindrome");
}
else {
System.out.println(usrInput + " is not a palindrome");
}
scnr.close();
}
}

44
word-freq/wordFreq.java Normal file
View File

@ -0,0 +1,44 @@
import java.util.Scanner;
/*
* input: 5 hey hi Mark hi mark
* output:
* hey 1
* hi 2
* Mark 1
* hi 2
* mark 1
*
* assume input will always be less than 20 words
* hint: Use two arrays, one array for strings
* one array for frequencies
*/
public class wordFreq {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String[] words = new String[20];
int[] frequencies = new int[20];
int numberOfWords = scnr.nextInt();
for (int i=0; i<numberOfWords; i++){
words[i] = scnr.next();
}
for (int i=0; i<numberOfWords; i++){
for (int j=0; j<numberOfWords; j++){
if (words[i].equals(words[j])){
frequencies[i]++;
}
}
}
for (int i=0; i<numberOfWords; i++){
System.out.println(words[i] + " " + frequencies[i]);
}
scnr.close();
}
}