Вы находитесь на странице: 1из 6

Ma. Marivic D.

Olaso BSIT - 112

Task Performance in Java (Pre-finals)


import java.util.Scanner;

import java.lang.*;

public class TaskPerf1 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Please enter a sentence:");

String sent = sc.nextLine();

System.out.println("Enter an Index so we can remove the letter on that index");

int index = sc.nextInt();

StringBuffer remove = new StringBuffer(sent);

System.out.println("Given String: " + sent);

System.out.println("Index Number: " +index);

int x = index + 3;

if(x >= sent.length()) {

remove.deleteCharAt(index);

System.out.println("New String: " + remove);

} else {

remove.deleteCharAt(index);

remove.deleteCharAt(x);

System.out.println("New String: " + remove);

}
Output

REPEAT ALL CHARACTER THRICE

CODINGS

importjava.util.Scanner;
publicclass TaskPerf2 {

public String thrice(String sample) {

intl = sample.length();
String classify = "";
for (inti = 0; i<l; i++)
{
classify += sample.substring(i,i+1) + sample.substring(i, i+1) + sample.substring(i, i+1);
}
returnclassify;

publicstaticvoidmain(String[] args) {

TaskPerf2 tp = new TaskPerf2();


Scanner sc = newScanner(System.in);
System.out.println("Please input a string so we can repeat the characters thrice:");
String sent = sc.nextLine();
System.out.println("The given string is: "+sent);
System.out.println("The new string is: "+tp.thrice(sent));
}

}
Output

REMOVE A LETTER EXCEPT FOR THE FIRST AND LAST

CODINGS

importjava.util.Scanner;
publicclass TaskPerf3 {

publicstaticcharindex;
publicstaticvoidmain(String[] args) {
TaskPerf3 qq = new TaskPerf3();
Scanner sc = newScanner(System.in);
System.out.println("Please enter a sentence:");
String sent = sc.nextLine();
System.out.println("Enter a letter so we can remove the letter on that index");
index = sc.next().charAt(0);

System.out.println("Given String: " + sent);


System.out.println("Letter to remove: " +index);
System.out.println("New String:" + qq.removeChar(sent));

}
public String removeChar(String sent1) {
String finalString = "";
intl = sent1.length();
for(inti = 0; i<l; i++) {
chartest = sent1.charAt(i);
if(! (i> 0 &&i<l - 1 &&test == index))
finalString = finalString + test;

}
returnfinalString;
}

Output

GUESS THE WORD

CODINGS

import java.util.Scanner;

import javax.swing.JOptionPane;

public class GuessTheWord {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

int trial = 0;

int maxTries = 8;

int counter = 8;

String secret = "The Joker";

String tries = "";

int superb = secret.length();

System.out.println("You have to guess the given word of the admin and you only have 9 tries:");

System.out.println("The total characters of the word are:" + superb);

System.out.println("This is the first letter:" + secret.charAt(0));

System.out.println("This is the last letter:" + secret.charAt(8));

do {

System.out.println("Enter the word that you think is the answer:");

tries = sc.nextLine();

if(tries.equals(secret)) {

System.out.println("Correct! Your guess matches the given word" + secret);

break;

else {

counter--;

System.out.println("Incorrect! The word you enter does not match the secret
game. Please try again.");

System.out.println("This is the number of attempts:" + counter);

}while(tries != secret && ++trial <maxTries);

if(trial == maxTries) {

System.out.println("You lose. The word is: " + secret);


}

If you got the correct answer the answer is this:

But if you don’t: