This commit is contained in:
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
}
}