0% found this document useful (0 votes)
40 views

LAB # 01 Introductory To Java String ADT and I/O Classes, Wrapper Classes and Filing in JAVA

This document describes a lab assignment on Java string handling, input/output, and file I/O. It includes 4 tasks: 1. Write a program to take user input of name, section, and GPA and print it. 2. Write a program that initializes 5 strings, concatenates them, converts one to uppercase and another to lowercase, and extracts substrings. 3. Write a program that takes 3 numbers as input, writes them to a file, reads from the file and prints the numbers. 4. Write a program that takes 3 numbers as input, writes them to a file as characters, reads from the file and prints the characters.

Uploaded by

Hammad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

LAB # 01 Introductory To Java String ADT and I/O Classes, Wrapper Classes and Filing in JAVA

This document describes a lab assignment on Java string handling, input/output, and file I/O. It includes 4 tasks: 1. Write a program to take user input of name, section, and GPA and print it. 2. Write a program that initializes 5 strings, concatenates them, converts one to uppercase and another to lowercase, and extracts substrings. 3. Write a program that takes 3 numbers as input, writes them to a file, reads from the file and prints the numbers. 4. Write a program that takes 3 numbers as input, writes them to a file as characters, reads from the file and prints the characters.

Uploaded by

Hammad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Lab # 01 SSUET/QR/114

LAB # 01

Introductory to Java String ADT and I/O Classes, Wrapper Classes


and Filing in JAVA

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";

String first = One.concat(Two);


String second = first.concat(three);
String third = second.concat(four);
String fourth = third.concat(five);

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

public class TASK4 {

public static void main(String[] args) {

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

OutputStream os = new FileOutputStream(f);


for(int i=0; i < bWrite.length ; i++){
os.write( bWrite[i]);
}
os.close();

InputStream is = new FileInputStream(f);


int size = is.available();

for(int i=0; i< size; i++){


System.out.print(size);
}
is.close();
}

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 {

public static void main(String[] args) {

File f;

try{
Scanner in = new Scanner(System.in);
byte a,b,c;
SE2020F-257 5|
Page
Lab # 01 SSUET/QR/114

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");
OutputStream os = new FileOutputStream(f);
for(int i=0; i < bWrite.length ; i++){
os.write( bWrite[i]);
}
os.close();

InputStream is = new FileInputStream(f);


int size = is.available();

for(int i=0; i< size; i++){


System.out.print((char)is.read() + " ");
}
is.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:

Enter the secret word: test


----
Guess a letter: a
----
Guess a letter: e
-e--
Guess a letter: n
-e--
Guess a letter: s
-es-
Guess a letter: t test=test
You guessed the word in 5 guesses.

CODE
import java.util.Scanner;

public class hangman {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

while (count < 7 && letter.contains("*")) {


System.out.println("Guess any letter in the word");
System.out.println(letter);
String guess = sc.next();
hang(guess);
}
sc.close();
}

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;

public static void hang(String guess) {


String newletter = "";
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == guess.charAt(0)) {
newletter += guess.charAt(0);

SE2020F-257 7|
Page
Lab # 01 SSUET/QR/114

} else if (letter.charAt(i) != '*') {


newletter += word.charAt(i);
} else {
newletter += "*";
}
}

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." );
}
}

public static void hangmanImage() {


if (count == 1) {
System.out.println("Wrong guess, try again");

}
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!");

System.out.println("GAME OVER! The word was " + word);


}

}
}

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

3. Write a program that print total numbers of words in a file

CODE
import java.io.*;

public class FileRead


{
public static void main(String[] args)
{
try{
File f=new File("C:\\Users\\dell\\Desktop\\file.txt");
BufferedReader br=new BufferedReader(new FileReader(f));
String i;
int count = 0;
while
((i=br.readLine())!=null)
{
String[]words= i.split(" ");
count=words.length;
}
br.close();
System.out.println("Total no of names:"+count);
}
catch(Exception e){
e.printStackTrace();
}
}
}
OUTPUT

SE2020F-257 11 |
Page

You might also like