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

JSJSJ

The document describes two Java classes - ContactApp and FireAir - that model contact and music applications. ContactApp allows the user to display all contacts, search for a contact by name, add a new contact, update an existing contact, and delete a contact. FireAir allows the user to print all songs, add a new song, delete a song by number, search for a song by name or number, and update a song's number or name.

Uploaded by

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

JSJSJ

The document describes two Java classes - ContactApp and FireAir - that model contact and music applications. ContactApp allows the user to display all contacts, search for a contact by name, add a new contact, update an existing contact, and delete a contact. FireAir allows the user to print all songs, add a new song, delete a song by number, search for a song by name or number, and update a song's number or name.

Uploaded by

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

DSA Practical, Dept of SWE, Mehran UET

Student Name Muhammad Saad


Roll Number 21SW112
Section # II
Lab # 2

Task#01

The contact app on our phone contains a lot of contacts. In ContactApp(class)

perform the following operations:

Display all contact

Search a contact //name -> number

Add a new contact // name, number , pos/index

Update the contact //name1, name2

Delete any contact //

Code:
import java.util.Scanner;

public class ContactApp {


Scanner scan=new Scanner(System.in);
static int size=10;
String[][] contacts=new String[size][2];
static int index;
void displayAllContacts(){
System.out.println("Name Mobile Number");
for (String[] contact : contacts) {
System.out.println(contact[0] + " " + contact[1]);
}
}
boolean searchContact(String name){
index=-1;
boolean found=false;
for (String[] contact : contacts) {
index++;
if (name.equals(contact[0])) {
found = true;
break;
}
}
return found;
}

void addNewContact(String name,String mobile){


DSA Practical, Dept of SWE, Mehran UET

try{
for (int i=0;i<contacts.length;i++) {
if(contacts[i][0]==null){
contacts[i][0]=name;
contacts[i][1]=mobile;
break;
}
}
}catch (ArrayIndexOutOfBoundsException e){
System.out.print("Storage is full!");
}
}
void updateContact() {
System.out.println("Enter name of contact you want to update: ");
String name1=scan.nextLine();
if(searchContact(name1)){
System.out.println("1->Change name: \n2->Change mobile number: ");
String choice=scan.nextLine();
if(choice.equals("1")){
System.out.print("Enter new name: ");
String updatedName=scan.nextLine();
contacts[index][0]=updatedName;
}else if(choice.equals("2")){
System.out.print("Enter new number: ");
String no= scan.nextLine();
contacts[index][1]=no;
}
}else System.out.println("Contact not found! ");

void deleteContact(String name){


if(searchContact(name)){
for(int i=index;i>0;i--){
contacts[i][0]=contacts[i-1][0];
contacts[i][1]=contacts[i-1][1];
}
contacts[0][0]=null;
contacts[0][1]=null;
}else System.out.println("Contact not found!");
}
}
public class Task2lab {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ContactApp app = new ContactApp();
app.contacts[0][0] = "laj";
app.contacts[0][1] = "03106551014";
app.contacts[1][0] = "Muqudas";
app.contacts[1][1] = "03045678909";
app.contacts[2][0] = "Urooj";
app.contacts[2][1] = "03063488379";
app.contacts[3][0] = "Anoosha";
app.contacts[3][1] = "032481239454";
app.contacts[4][0] = "Kashmala";
app.contacts[4][1] = "03151345921";
String choice;
do {
System.out.println("***Contact App***\n Created By \"SDB\"\n\n");
System.out.println("1->Display All Contacts");
System.out.println("2->Search a contact");
System.out.println("3->Add a new contact");
DSA Practical, Dept of SWE, Mehran UET

System.out.println("4->Update the contact");


System.out.println("5->Delete any contact");
System.out.println("0->To exit");
choice = scan.nextLine();
String name, number;
for(int i=0;i<30;i++) System.out.println();
switch (choice) {
case "1" -> {
app.displayAllContacts();
System.out.print("Press any key");
String temp=scan.nextLine();
}
case "2" -> {
System.out.print("Enter name to search: ");
name = scan.nextLine();
if (app.searchContact(name)) System.out.println("Contact Found
with number "+app.contacts[ContactApp.index][1]);
else System.out.println("Contact not found!");
System.out.print("Press any key");
String temp=scan.nextLine();
}
case "3" -> {
System.out.print("Enter number to add: ");
number = scan.nextLine();
System.out.print("Enter name to save this contact: ");
name = scan.nextLine();
app.addNewContact(name, number);
System.out.print("Press any key");
String temp=scan.nextLine();
}
case "4" -> {
app.updateContact();
System.out.print("Press any key");
String temp = scan.nextLine();
}
case "5" -> {
System.out.print("Enter name to delete the contact: ");
name = scan.nextLine();
app.deleteContact(name);
System.out.print("Press any key");
String temp = scan.nextLine();
}
}
for (int i=0;i<50;i++) System.out.println();
}while (!Objects.equals(choice, "0"));
}
}

Output:
DSA Practical, Dept of SWE, Mehran UET

TASK#2
Task#02: The Music app name is FireAir(class) music app which can
perform following functions

print all the array songs/music names one by one.


Adds new songs/music.
Deletes a song/music using the number.
Searches song/music using a number or by the name.
Update song/music (index, value)

CODE
import java.util.Scanner;

public class FireAir {


Scanner scan=new Scanner(System.in);
int size=10;
static int index;
String[][] songs;
public void printAllSongs(){
System.out.println("Song# song\n");
for (String[] song:songs) {
System.out.println(song[0]+" "+song[1]);
}
}
public void addNewSong(String song){
for (int i=0;i< songs.length;i++){
if(songs[i][0]==null){
songs[i][0]=String.valueOf(i+1);
songs[i][1]=song;
break;
}
}
}
public void deleteSong(int number){
DSA Practical, Dept of SWE, Mehran UET

for (int i=number-1;i<songs.length;i++){


if (songs[i][0]!=null){
songs[i][0]=songs[i+1][0];
songs[i][1]=songs[i+1][1];
}
}
}
public boolean searchSong(String song){
index=-1;
for (String[] strings : songs){
index++;
if (song.equals(strings[1]) || song.equals(strings[0])) return true;
}
return false;
}
public void updateSong(String song){
if(searchSong(song)){
System.out.println("1->Update serial number\n2->Update name");
String update=scan.nextLine();
if(update.equals("1")){
System.out.print("Enter new number to change the previous number:
");
update=scan.nextLine();
songs[index][0]=update;
} else if (update.equals("2")) {

System.out.print("Enter new name to change the previous name: ");


update=scan.nextLine();
songs[index][1]=update;
}
System.out.println("Song updates successfully.");
}
else System.out.println("Song not found!");
}
}
import java.util.Objects;
import java.util.Scanner;

public class Task_Lab2 {


public static void main(String[] args) {
FireAir obj=new FireAir();
obj.songs=new String[obj.size][2];
obj.songs[0][0]="1";
obj.songs[0][1]="Song1";
obj.songs[1][0]="2";
obj.songs[1][1]="Song2";
obj.songs[2][0]="3";
obj.songs[2][1]="Song3";
obj.songs[3][0]="4";
obj.songs[3][1]="Song4";
obj.songs[4][0]="5";
obj.songs[4][1]="Song5";

Scanner scan=new Scanner(System.in);


String choice;
do {
System.out.println("*** Music Player ***\n Created By \"SDB\"\n\n");
System.out.println("1->Display All Songs");
System.out.println("2->Add a new song");
System.out.println("3->Delete a song");
System.out.println("4->Searching a song");
System.out.println("5->Update a song");
DSA Practical, Dept of SWE, Mehran UET

System.out.println("0->To exit");
choice = scan.nextLine();
String name, number;
for(int i=0;i<30;i++) System.out.println();
switch (choice) {
case "1" -> {
obj.printAllSongs();
System.out.print("Press any key");
String temp=scan.nextLine();
}
case "2" -> {
System.out.print("Enter song name to add: ");
name=scan.nextLine();
obj.addNewSong(name);
System.out.println("New song added successfully.");
System.out.print("Press any key");
String temp=scan.nextLine();
}
case "3" -> {
System.out.print("Enter serial number of the song to delete:
");
name = scan.nextLine();
obj.deleteSong(Integer.parseInt(name));
System.out.println("Song with Serial number "+name+" deleted
successfully.");
System.out.print("Press any key");
String temp=scan.nextLine();
}
case "4" -> {
System.out.print("1->Search by serial number: \n2->Search by
song name: \n");
number = scan.nextLine();
if (number.equals("1")) {
System.out.print("Enter serial number: ");
name = scan.nextLine();
if (obj.searchSong(name))
System.out.println("Song with serial number " + name +
" is " + obj.songs[FireAir.index][1]);
else System.out.println("Song not found!");
} else if (number.equals("2")) {
System.out.print("Enter name of the song: ");
name = scan.nextLine();
if (obj.searchSong(name))
System.out.println("Song with name " + name + " is
found Its serial number is " + obj.songs[FireAir.index][0]);
else System.out.println("Song not found!");
}
System.out.print("Press any key");
String temp = scan.nextLine();
}
case "5" -> {
System.out.print("Enter serial number or name of the song to
update : ");
name = scan.nextLine();
obj.updateSong(name);
System.out.print("Press any key");
String temp = scan.nextLine();
}
}
for (int i=0;i<50;i++) System.out.println();
}while (!Objects.equals(choice, "0"));
DSA Practical, Dept of SWE, Mehran UET

}
}

OUTPUT:

You might also like