This commit is contained in:
k0rrluna 2024-12-31 14:58:42 +03:00
parent db2dbc5c19
commit b050db6dce
16 changed files with 323 additions and 1 deletions

35
labs/lab10/Backwards.java Normal file
View File

@ -0,0 +1,35 @@
// ******************************************************************
// Backwards.java
//
// Uses a recursive method to print a string backwards.
// ******************************************************************
import java.util.Scanner;
public class Backwards
{
//--------------------------------------------------------------
// Reads a string from the user and prints it backwards.
//--------------------------------------------------------------
public static void main(String[] args)
{
String msg;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a string: ");
msg = scan.nextLine();
System.out.print("\nThe string backwards: ");
printBackwards(msg);
System.out.println();
}
//--------------------------------------------------------------
// Takes a string and recursively prints it backwards.
//--------------------------------------------------------------
public static void printBackwards(String s)
{
if (s.isEmpty()) {
return;
}
System.out.print(s.charAt(s.length() - 1));
printBackwards(s.substring(0, s.length() - 1));
// Fill in code
}
}

View File

@ -0,0 +1,36 @@
// ****************************************************************
// CountLetters.java
//
// Reads a words from the standard input and prints the number of
// occurrences of each letter in that word.
//
// ****************************************************************
import java.util.Scanner;
public class CountLetterse
{
public static void main(String[] args)
{
int[] counts = new int[26];
Scanner scan = new Scanner(System.in);
//get word from user
System.out.print("Enter a single word (letters only, please): ");
String word = scan.nextLine();
//convert to all upper case
word = word.toUpperCase();
//count frequency of each letter in string
for (int i=0; i < word.length(); i++) {
try {
char current = word.charAt(i);
counts[word.charAt(i)-'A']++;
}
catch (Exception e){
System.out.println("Reason for error:"+"!"+(char)word.charAt(i)+"!");
}
}
//print frequencies
System.out.println();
for (int i=0; i < counts.length; i++)
if (counts [i] != 0)
System.out.println((char)(i +'A') + ": " + counts[i]);
}
}

View File

@ -0,0 +1,33 @@
// ****************************************************************
// CountLetters.java
//
// Reads a words from the standard input and prints the number of
// occurrences of each letter in that word.
//
// ****************************************************************
import java.util.Scanner;
public class CountLetters
{
public static void main(String[] args)
{
int[] counts = new int[26];
Scanner scan = new Scanner(System.in);
//get word from user
System.out.print("Enter a single word (letters only, please): ");
String word = scan.nextLine();
//convert to all upper case
word = word.toUpperCase();
//count frequency of each letter in string
for (int i=0; i < word.length(); i++) {
char current = word.charAt(i);
if (current >= 'A' && current <= 'Z') {
counts[word.charAt(i)-'A']++;
}
}
//print frequencies
System.out.println();
for (int i=0; i < counts.length; i++)
if (counts [i] != 0)
System.out.println((char)(i +'A') + ": " + counts[i]);
}
}

42
labs/lab10/Power.java Normal file
View File

@ -0,0 +1,42 @@
// ****************************************************************
// Power.java
//
// Reads in two integers and uses a recursive power method
// to compute the first raised to the second power.
// ****************************************************************
import java.util.Scanner;
public class Power
{
public static void main(String[] args)
{
int base, exp;
int answer;
Scanner scan = new Scanner(System.in);
System.out.print("Welcome to the power program! ");
System.out.println("Please use integers only.");
//get base
System.out.print("Enter the base you would like raised to a power: ");
base = scan.nextInt();
//get exponent
System.out.print("Enter the power you would like it raised to: ");
exp = scan.nextInt();
answer = power(base,exp);
System.out.println(base + " raised to the " + exp + " is " + answer);
}
// -------------------------------------------------
// Computes and returns base^exp
// -------------------------------------------------
public static int power(int base, int exp)
{
int pow;
if (exp == 0) {
pow = 1;
} else {
pow = base*power(base, exp-1);
}
return pow;
//otherwise set pow to base*base^(exp-1)
//return pow
}
}

Binary file not shown.

View File

@ -9,6 +9,7 @@ public class Account
private double balance;
private String name;
private long acctNum;
private static int numAccounts = 0;
//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
@ -17,15 +18,18 @@ public class Account
balance = initBal;
name = owner;
acctNum = number;
++numAccounts;
}
public Account(double initBal, String owner)
{
balance = initBal;
name = owner;
++numAccounts;
}
public Account(String owner)
{
name = owner;
++numAccounts;
}
//----------------------------------------------
// Checks to see if balance is sufficient for withdrawal.
@ -59,6 +63,16 @@ public class Account
{
return balance;
}
// Static
public static int getNumAccounts()
{
return numAccounts;
}
public void close () {
name += " Closed";
}
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
@ -66,6 +80,7 @@ public class Account
{
return "Name:" + name +
"\nAccount Number: " + acctNum +
"\nBalance: " + balance;
"\nBalance: " + balance;
//"\nTotal account number: " + numAccounts;
}
}

BIN
labs/lab7/IntList.class Normal file

Binary file not shown.

50
labs/lab7/IntList.java Normal file
View File

@ -0,0 +1,50 @@
// ****************************************************************
// IntList.java
//
// An (unsorted) integer list class with a method to add an
// integer to the list and a toString method that returns the contents
// of the list with indices.
// ****************************************************************
public class IntList {
protected int[] list;
protected int numElements = 0;
//-------------------------------------------------------------
// Constructor -- creates an integer list of a given size.
//-------------------------------------------------------------
public IntList(int size) {
list = new int[size];
}
//-------------------------------------------------------------
// Adds an integer to the list. If the list is full,
// prints a message and does nothing.
//-------------------------------------------------------------
public void add(int value) {
if (numElements == list.length) {
System.out.println("Can't add, list is full");
} else {
list[numElements] = value;
numElements++;
}
}
//-------------------------------------------------------------
// Returns the list elements with indices as a string.
//-------------------------------------------------------------
@Override
public String toString() {
StringBuilder result = new StringBuilder();
for (int i = 0; i < numElements; i++) {
result.append(i).append(": ").append(list[i]).append("\n");
}
return result.toString();
}
}
// -------------------------------------------------------------
// Interface for sorting numbers.
// -------------------------------------------------------------
interface NumberSorter {
void sortNumbers();
}

BIN
labs/lab7/ListTest.class Normal file

Binary file not shown.

29
labs/lab7/ListTest.java Normal file
View File

@ -0,0 +1,29 @@
// ****************************************************************
// ListTest.java
//
// A simple test program that creates an IntList, puts some
// ints in it, and prints the list.
//
// ****************************************************************
public class ListTest
{
public static void main(String[] args)
{
IntList myList = new IntList(10);
myList.add(100);
myList.add(50);
myList.add(200);
myList.add(25);
System.out.println(myList);
IntList intList = new IntList(10);
intList.add(10);
intList.add(5);
intList.add(20);
intList.add(15);
// Print the unsorted list
System.out.println("IntList contents:");
System.out.println(intList);
}
}

Binary file not shown.

43
labs/lab7/Player.java Normal file
View File

@ -0,0 +1,43 @@
// **********************************************************
// Player.java
//
// Defines a Player class that holds information about an athlete.
// **********************************************************
import java.util.Scanner;
public class Player
{
private String name;
private String team;
private int jerseyNumber;
//-----------------------------------------------------------
// Prompts for and reads in the player's name, team, and
// jersey number.
//-----------------------------------------------------------
public void readPlayer()
{
Scanner scan = new Scanner(System.in);
System.out.print("Name: ");
name = scan.nextLine();
System.out.print("Team: ");
team = scan.nextLine();
System.out.print("Jersey number: ");
jerseyNumber = Scan.nextInt();
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true; // Check if the two references are the same
if (obj == null || getClass() != obj.getClass()) return false; // Ensure obj is a Player
Player other = (Player) obj; // Typecast obj to Player
return team.equals(other.team) && jerseyNumber == other.jerseyNumber;
}
//-----------------------------------------------------------
// Returns a string representation of the player.
//-----------------------------------------------------------
@Override
public String toString() {
return "Name: " + name + ", Team: " + team + ", Jersey Number: " + jerseyNumber;
}
}

Binary file not shown.

View File

@ -0,0 +1,39 @@
// ****************************************************************
// SortedIntList.java
//
// A subclass of IntList that sorts the numbers in descending order.
// ****************************************************************
class SortedIntList extends IntList implements NumberSorter {
//-------------------------------------------------------------
// Constructor -- creates an integer list of a given size.
//-------------------------------------------------------------
public SortedIntList(int size) {
super(size);
}
//-------------------------------------------------------------
// Sorts the numbers in descending order.
//-------------------------------------------------------------
@Override
public void sortNumbers() {
for (int i = 0; i < numElements - 1; i++) {
for (int j = 0; j < numElements - i - 1; j++) {
if (list[j] < list[j + 1]) {
// Swap list[j] and list[j + 1]
int temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
}
//-------------------------------------------------------------
// Adds an integer to the list and sorts it.
//-------------------------------------------------------------
@Override
public void add(int value) {
super.add(value);
sortNumbers();
}
}

Binary file not shown.