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

java slip 4

The document contains two Java programs: one for creating a blinking text effect using Swing components, and another for managing city STD codes with functionalities to add, remove, and search for cities. The BlinkingText class uses a separate thread to toggle the visibility of a JLabel, while the CitySTDCodeManager class utilizes a HashMap to store city names and their corresponding STD codes, providing a command-line interface for user interaction. Both programs demonstrate basic Java programming concepts such as threading, GUI development, and data structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

java slip 4

The document contains two Java programs: one for creating a blinking text effect using Swing components, and another for managing city STD codes with functionalities to add, remove, and search for cities. The BlinkingText class uses a separate thread to toggle the visibility of a JLabel, while the CitySTDCodeManager class utilizes a HashMap to store city names and their corresponding STD codes, providing a command-line interface for user interaction. Both programs demonstrate basic Java programming concepts such as threading, GUI development, and data structures.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q1

import javax.swing.*;
import java.awt.*;

public class BlinkingText implements Runnable {


private JLabel label;

public BlinkingText(JLabel label) {


this.label = label;
}

@Override
public void run() {
try {
while (true) {
Thread.sleep(500); // Blink interval
label.setVisible(!label.isVisible());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public static void main(String[] args) {


JFrame frame = new JFrame("Blinking Text Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
frame.setLayout(new FlowLayout());

JLabel label = new JLabel("Blinking Text");


label.setFont(new Font("Arial", Font.BOLD, 24));
frame.add(label);

Thread blinkThread = new Thread(new BlinkingText(label));


blinkThread.start();

frame.setVisible(true);
}
}

Q2

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class CitySTDCodeManager {


private Map<String, String> cityCodes;

public CitySTDCodeManager() {
cityCodes = new HashMap<>();
}

// Add a new city and its STD code (No duplicates)


public void addCity(String city, String code) {
if (!cityCodes.containsKey(city)) {
cityCodes.put(city, code);
System.out.println("City added successfully.");
} else {
System.out.println("City already exists!");
}
}

// Remove a city from the collection


public void removeCity(String city) {
if (cityCodes.remove(city) != null) {
System.out.println("City removed successfully.");
} else {
System.out.println("City not found!");
}
}

// Search for a city name and display the code


public void searchCity(String city) {
if (cityCodes.containsKey(city)) {
System.out.println("STD Code for " + city + " is: " +
cityCodes.get(city));
} else {
System.out.println("City not found!");
}
}

public static void main(String[] args) {


CitySTDCodeManager manager = new CitySTDCodeManager();
Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("\nChoose an option:");
System.out.println("1. Add City");
System.out.println("2. Remove City");
System.out.println("3. Search City");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");

int choice = scanner.nextInt();


scanner.nextLine(); // Consume newline

switch (choice) {
case 1:
System.out.print("Enter city name: ");
String city = scanner.nextLine();
System.out.print("Enter STD code: ");
String code = scanner.nextLine();
manager.addCity(city, code);
break;
case 2:
System.out.print("Enter city name to remove: ");
city = scanner.nextLine();
manager.removeCity(city);
break;
case 3:
System.out.print("Enter city name to search: ");
city = scanner.nextLine();
manager.searchCity(city);
break;
case 4:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice! Try again.");
}
}
}
}

You might also like