assignment4

This commit is contained in:
k0rrluna 2024-12-20 03:17:36 +03:00
parent 337d5b0c16
commit e07eec00f8
7 changed files with 203 additions and 0 deletions

View File

@ -0,0 +1,32 @@
import java.util.Scanner;
import java.util.ArrayList;
public class numbers {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
Scanner scan = new Scanner(System.in);
int count = 0;
System.out.println("Please enter How many integer in list ?");
count = scan.nextInt();
int temp1 = count;
while (temp1 > 0) {
System.out.println("Enter integers !");
int i = scan.nextInt();
numbers.add(i);
temp1--;
}
System.out.println(numbers);
temp1 = count-1;
int j = 0;
while(0 < temp1/2) {
int temp = numbers.get(j);
numbers.set(j, numbers.get(temp1));
numbers.set(temp1, temp);
j++;
temp1--;
}
System.out.println(numbers);
}
}

View File

@ -0,0 +1,48 @@
## IF STATEMENT
import java.util.Scanner;
import java.text.NumberFormat;
public class Salary
{
public static void main (String[] args)
{
double currentSalary; // employee's current salary
double raise = 0; // amount of the raise (initialized to 0)
double newSalary; // new salary for the employee
String rating; // performance rating
Scanner scan = new Scanner(System.in);
System.out.print("Enter the current salary: ");
currentSalary = scan.nextDouble();
// Consume the newline character left by nextDouble
scan.nextLine();
System.out.print("Enter the performance rating (Excellent, Good, or Poor): ");
rating = scan.nextLine();
// Compute the raise using if-else
if (rating.equals("Excellent")) {
raise = currentSalary * 0.06; // 6% raise for Excellent
} else if (rating.equals("Good")) {
raise = currentSalary * 0.04; // 4% raise for Good
} else if (rating.equals("Poor")) {
raise = currentSalary * 0.015; // 1.5% raise for Poor
} else {
System.out.println("Invalid rating entered.");
return; // Exit the program if the rating is not valid
}
// Compute the new salary
newSalary = currentSalary + raise;
// Print the results
NumberFormat money = NumberFormat.getCurrencyInstance();
System.out.println();
System.out.println("Current Salary: " + money.format(currentSalary));
System.out.println("Amount of your raise: " + money.format(raise));
System.out.println("Your new salary: " + money.format(newSalary));
System.out.println();
}
}

View File

@ -0,0 +1,2 @@
[ZoneTransfer]
ZoneId=3

View File

@ -0,0 +1,86 @@
//FOR LOOP & SWITCH-CASE
import java.util.Scanner;
public class Count
{
public static void main (String[] args)
{
String phrase; // a string of characters
int countBlank; // the number of blanks (spaces) in the phrase
int countA; // the number of 'a' and 'A'
int countE; // the number of 'e' and 'E'
int countS; // the number of 's' and 'S'
int countT; // the number of 't' and 'T'
int length; // the length of the phrase
char ch; // an individual character in the string
Scanner scan = new Scanner(System.in);
// Print a program header
System.out.println ();
System.out.println("Character Counter");
System.out.println();
// Outer while loop to keep asking for phrases until "quit"
while (true) {
System.out.print("Enter a sentence or phrase (or type 'quit' to exit): ");
phrase = scan.nextLine();
// Exit if user types "quit"
if (phrase.equalsIgnoreCase("quit")) { // Capital-lower letter ignore
break;
}
// Initialize counts for each new phrase
countBlank = 0;
countA = 0;
countE = 0;
countS = 0;
countT = 0;
// Get the length of the phrase
length = phrase.length();
// Loop through each character in the phrase
for (int i = 0; i < length; i++) {
ch = phrase.charAt(i); // get character at position i
// Use switch statement to count occurrences of specific characters
switch (ch) {
case 'a':
case 'A':
countA++;
break;
case 'e':
case 'E':
countE++;
break;
case 's':
case 'S':
countS++;
break;
case 't':
case 'T':
countT++;
break;
case ' ':
countBlank++; // count spaces
break;
default:
// Do nothing for other characters
break;
}
}
// Print the results
System.out.println();
System.out.println("Number of blank spaces: " + countBlank);
System.out.println("Number of 'a'/'A': " + countA);
System.out.println("Number of 'e'/'E': " + countE);
System.out.println("Number of 's'/'S': " + countS);
System.out.println("Number of 't'/'T': " + countT);
System.out.println();
}
System.out.println("Goodbye!"); // Print a goodbye message when exiting
}
}

View File

@ -0,0 +1,2 @@
[ZoneTransfer]
ZoneId=3

31
labs/lab5/WHILE LOOP.txt Normal file
View File

@ -0,0 +1,31 @@
// WHILE LOOP
import java.util.Scanner;
public class PowersOf2
{
public static void main(String[] args)
{
int numPowersOf2; // How many powers of 2 to compute
int nextPowerOf2 = 1; // Current power of 2 (starting with 2^0 = 1)
int exponent = 0; // Exponent for current power of 2 (starting with 0)
Scanner scan = new Scanner(System.in);
System.out.println("How many powers of 2 would you like printed?");
numPowersOf2 = scan.nextInt();
// Print a message saying how many powers of 2 will be printed
System.out.println("Here are the first " + numPowersOf2 + " powers of 2:");
// Print the powers of 2
while (exponent < numPowersOf2)
{
System.out.println("2^" + exponent + " = " + nextPowerOf2);
// Compute the next power of 2 by doubling the current one
nextPowerOf2 *= 2;
// Increment exponent
exponent++;
}
}
}

View File

@ -0,0 +1,2 @@
[ZoneTransfer]
ZoneId=3