ns4
Q1]
import javax.swing.;
import java.awt.;
import java.awt.event.*;
public class BlinkingText implements Runnable {
private JLabel label;
public BlinkingText() {
JFrame frame = new JFrame("Blinking Text");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 200);
label = new JLabel("Blinking Text");
label.setFont(new Font("Arial", Font.BOLD, 20));
frame.add(label, BorderLayout.CENTER);
frame.setVisible(true);
}
@Override
public void run() {
try {
while (true) {
Thread.sleep(500);
label.setVisible(!label.isVisible());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
BlinkingText blinkingText = new BlinkingText();
Thread thread = new Thread(blinkingText);
thread.start();
}
}
Q2]
import java.util.*;
public class CitySTDCode {
private Map<String, Integer> citySTDMap;
public CitySTDCode() {
citySTDMap = new HashMap<>();
}
public void addCity(String cityName, int stdCode) {
if (!citySTDMap.containsKey(cityName)) {
citySTDMap.put(cityName, stdCode);
System.out.println("City '" + cityName + "' added with STD code: " + stdCode);
} else {
System.out.println("City '" + cityName + "' already exists in the collection.");
}
}
public void removeCity(String cityName) {
if (citySTDMap.containsKey(cityName)) {
int stdCode = citySTDMap.remove(cityName);
System.out.println("City '" + cityName + "' with STD code " + stdCode + " removed from the collection.");
} else {
System.out.println("City '" + cityName + "' not found in the collection.");
}
}
public void searchCity(String cityName) {
if (citySTDMap.containsKey(cityName)) {
int stdCode = citySTDMap.get(cityName);
System.out.println("STD code for city '" + cityName + "': " + stdCode);
} else {
System.out.println("City '" + cityName + "' not found in the collection.");
}
}
public static void main(String[] args) {
CitySTDCode citySTDCode = new CitySTDCode();
citySTDCode.addCity("New York", 212);
citySTDCode.addCity("Los Angeles", 213);
citySTDCode.addCity("Chicago", 312);
citySTDCode.addCity("San Francisco", 415);
System.out.println("\nPerforming operations:");
citySTDCode.addCity("Los Angeles", 310);
citySTDCode.removeCity("Chicago");
citySTDCode.searchCity("New York");
citySTDCode.searchCity("Houston");
}
}