LAB # 01 Introductory To Java String ADT and I/O Classes, Wrapper Classes and Filing in JAVA
LAB # 01 Introductory To Java String ADT and I/O Classes, Wrapper Classes and Filing in JAVA
LAB # 01
Object
To implement Java Built-In String Class, User input and Filing.
EXAMPLES
Sample Program#1
Sample Program# 2
Sample Program#3
SE2020F-257 1|
Page
Lab # 01 SSUET/QR/114
Lab Tasks
1. Write a program that takes your name, section and GPA as input and print it.
CODE
import java.util.Scanner;
public class NewClass {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
System.out.println("Enter your name: ");
String Name = input.next();
System.out.println("Enter your Section: ");
String Section = input.next();
System.out.println("Enter your GPA: ");
double GPA = input.nextDouble();
System.out.println("\n\tStudent Information");
System.out.println("Name: " + Name);
System.out.println("Section: " + Section);
System.out.println("GPA: " + GPA);
}
}
OUTPUT
SE2020F-257 2|
Page
Lab # 01 SSUET/QR/114
2. Write a program that initialize five different strings and perform the following operations. a.
Concatenate all five stings
b. Convert first one string to uppercase
c. Convert fourth string to lowercase
d. Find the substring from the concatenated string from 8 to onward
e. Find the substring from the concatenated string from 2 to onward
CODE
import java.io.*;
public class NewClass {
public static void main(String args[]){
// part a
String One = "Data";
String Two = "Structure";
String three = "Java";
String four = "Error";
String five = "Main";
System.out.println(fourth);
System.out.println(One.toUpperCase());
System.out.println(four.toLowerCase());
System.out.println(fourth.substring(8,fourth.length()));
System.out.println(fourth.substring(2,fourth.length()));
}
SE2020F-257 3|
Page
Lab # 01 SSUET/QR/114
OUTPUT
3.Write a program that takes three numbers as input write it on a file and then read from the file
and print it.
CODE
File f;
try{
Scanner in = new Scanner(System.in);
byte a,b,c;
System.out.print("Enter 1st Number: ");
a = in.nextByte();
System.out.print("Enter 2nd Number: ");
b = in.nextByte();
System.out.print("Enter 3rd Number: ");
c = in.nextByte();
byte bWrite[] = {a,b,c};
f = new File("C:\\Users\\dell\\Desktop/file.txt");
SE2020F-257 4|
Page
Lab # 01 SSUET/QR/114
OUTPUT
4. Write a program that takes three numbers as input write it on a file and then read from the file,
convert them into character and print it
CODE
public class TASK4 {
File f;
try{
Scanner in = new Scanner(System.in);
byte a,b,c;
SE2020F-257 5|
Page
Lab # 01 SSUET/QR/114
f = new File("C:\\Users\\dell\\Desktop/file.txt");
OutputStream os = new FileOutputStream(f);
for(int i=0; i < bWrite.length ; i++){
os.write( bWrite[i]);
}
os.close();
catch(IOException e){
System.out.print("Exception");
}
}
}
OUTPUT
1. Write a program that take string as an input attempting to guess the secret word enters letters
one at a time. After each guess, the guess template is updated (if necessary) to show which
letters in the secret word match the letter guessed. This process continues until the guess
SE2020F-257 6|
Page
Lab # 01 SSUET/QR/114
template matches the secret word choice of 7 wrong attempts. The number of guesses is
then output. For example:
CODE
import java.util.Scanner;
private static String[] words = {"pen", "pink", "class", "phone", "flower", "water" };
private static String word = words[(int) (Math.random() * words.length)];
private static String letter = new String(new char[word.length()]).replace("\0", "*");
private static int count = 0;
SE2020F-257 7|
Page
Lab # 01 SSUET/QR/114
if (letter.equals(newletter)) {
count++;
hangmanImage();
} else {
letter = newletter;
}
if (letter.equals(word)) {
System.out.println("Correct Answer! You win! The word was " + word + " you
guessed the word in "+count+"guesses." );
}
}
}
if (count == 2) {
System.out.println("Wrong guess, try again");
}
if (count == 3) {
System.out.println("Wrong guess, try again");
}
if (count == 4) {
System.out.println("Wrong guess, try again");
}
if (count == 5) {
System.out.println("Wrong guess, try again");
}
if (count == 6) {
System.out.println("Wrong guess, try again");
}
if (count == 7) {
SE2020F-257 8|
Page
Lab # 01 SSUET/QR/114
System.out.println("GAME OVER!");
}
}
OUTPUT
2. Write a program that read a names from the given file” Names” and searches for a specific
initial letter in the last name and write a new file “NewNames” with those searched names.
CODE
import java.io.*;
import java.util.Scanner;
public class FileRead
{
public static void main(String[] args)
{
SE2020F-257 9|
Page
Lab # 01 SSUET/QR/114
try{
Scanner input =new Scanner(System.in);
System.out.print ("Enter a letter:");
String n = input.next();
File f1=new File ("C:\\Users\\dell\\Desktop\\names.txt");
BufferedReader br = new BufferedReader(new FileReader(f1));
File f2=new File("C:\\Users\\dell\\Desktop\\NewNames.txt");
BufferedWriter bw=new BufferedWriter(new FileWriter(f2));
String i;
while((i=br.readLine())!=null)
{
if(i.indexOf(n)==0)
{
System.out.println(i);
bw.write(i);
}
}
br.close();
bw.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
OUTPUT
SE2020F-257 10 |
Page
Lab # 01 SSUET/QR/114
CODE
import java.io.*;
SE2020F-257 11 |
Page