Online compiler
unknown
html
a year ago
10 kB
14
Indexable
5d) traffic lights
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.*;
public class TrafficLight extends JFrame implements ActionListener {
    JButton b1, b2, b3;
      Signal green = new Signal(Color.green);
      Signal yellow = new Signal(Color.yellow);
      Signal red = new Signal(Color.red);
    public TrafficLight(){
        super("Traffic Light");
        getContentPane().setLayout(new GridLayout(2, 1));
        b1 = new JButton("Red");
        b2 = new JButton("Yellow");
        b3 = new JButton("Green");
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);        
        green.turnOn(false);
        yellow.turnOn(false);
        red.turnOn(false);
        JPanel p1 = new JPanel(new GridLayout(3,1));
        p1.add(red);
        p1.add(yellow);
        p1.add(green);
        JPanel p2 = new JPanel(new FlowLayout());
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        getContentPane().add(p1);
        getContentPane().add(p2);
        pack();
        }
    public static void main(String[] args){
        TrafficLight tl = new TrafficLight();        
        tl.setVisible(true);
    }    
    public void actionPerformed(ActionEvent e){        
TrafficBean tb=new TrafficBean();
        if (e.getSource() == b1){
tb.setRed(true);
red.turnOn(tb.getRed());
green.turnOn(false);            
yellow.turnOn(false);
        } else if (e.getSource() == b2){
tb.setYellow(true);
yellow.turnOn(tb.getYellow());
green.turnOn(false);
red.turnOn(false);
        } else if (e.getSource() == b3){
tb.setGreen(true);
green.turnOn(tb.getGreen());
red.turnOn(false);            
yellow.turnOn(false);
        }
    }
}     
class Signal extends JPanel{
    Color on;
    int radius = 40;
    int border = 10;
    boolean change;
    Signal(Color color){
        on = color;
        change = true;
    }
    public void turnOn(boolean a){
        change = a;
        repaint();        
    }
    public Dimension getPreferredSize(){
        int size = (radius+border)*2;
        return new Dimension( size, size );
    }
    public void paintComponent(Graphics g){
        g.setColor( Color.black );
        g.fillRect(0,0,getWidth(),getHeight());
        if (change){
            g.setColor( on );
        } else {
            g.setColor( on.darker().darker().darker() );
        }
        g.fillOval( border,border,2*radius,2*radius );
    }
}
public class TrafficBean
{
boolean r,g,y;
public TrafficBean()
{
}
public void setRed(boolean red)
{
	r=red;
}
public boolean getRed()
{
return r;
}
public void setGreen(boolean green)
{
	g=green;
}
public boolean getGreen()
{
return g;
}
public void setYellow(boolean yellow)
{
	y=yellow;
}
public boolean getYellow()
{
return y;
}
}
5c) calculator
public class CalcDemo {
    public static void main(String[] args) {
        CalcBeans calc = new CalcBeans();
        calc.setNum1(10);
        calc.setNum2(5);
        System.out.println("Addition: " + calc.add());
        System.out.println("Subtraction: " + calc.subtract());
        System.out.println("Multiplication: " + calc.multiply());
        System.out.println("Division: " + calc.divide());
    }
}
public class CalcBeans {
    private double num1;
    private double num2;
    public CalcBeans() {}
    public double getNum1() {
        return num1;
    }
    public void setNum1(double num1) {
        this.num1 = num1;
    }
    public double getNum2() {
        return num2;
    }
    public void setNum2(double num2) {
        this.num2 = num2;
    }
    public double add() {
        return num1 + num2;
    }
    public double subtract() {
        return num1 - num2;
    }
    public double multiply() {
        return num1 * num2;
    }
    public double divide() {
        if (num2 != 0) {
            return num1 / num2;
        } else {
            throw new ArithmeticException("Cannot divide by zero");
        }
    }
}
LAB EXPERIMENT-3
WEEK-3:
3. Using JavaScript:
a)	Implement Mouse Events
b)	Implement Keyboard Events
Implement Mouse Events Programs :-
Total no. of html files :-01
html code for mouse events :-
<html>
<head>
<title>JS Mouse Events</title>
</head>
<body>
<button id="btn">Click me with any mouse button to know the facts of life: left, right, middle,
...</button>
<p id="message"></p>
<script>
let btn = document.querySelector('#btn');
// disable context menu when right-mouse clicked btn.addEventListener('contextmenu', (e) => { e.preventDefault();
});
// show the mouse event message btn.addEventListener('mouseup', (e) => {
let msg = document.querySelector('#message'); switch (e.button) {
case 0:
msg.textContent = 'Greater things come to those who get off their ass and do anything to make it happen...';
break; case 1:
msg.textContent = 'Don’t be afraid to stand for what you believe in, even if that means standing alone...';
break; case 2:
msg.textContent = 'Never be afraid to trust an unknown future to a known God...'; break;
default:
msg.textContent = `Unknown mouse button code: ${event.button}`;
}
});
</script>
</body>
</html>
Implement Keyboard Events Programs :-
Total no. of html files :-01
html code for keyboard events :-
<html>
<head>
<title>Keyboard Events</title>
<style type="text/css">
p{font-family:Helvetica (sans-serif); background:#D489D0;
color:#fff;
padding:10px; border:2px solid #555;}
</style>
</head>
<body>
<form>
<p>
<label for="name"> Name:
<input autofocus id="name" name="name" /></label>
</p>
<p>
<label for="nick"> Nickname:
<input id="nick" name="nick" /></label>
</p>
<button type="submit">Submit</button>
</form>
<span id="output"></span>
</body>
<script>
var items = document.getElementsByTagName("input"); for (var i=0; i < items.length; i++){
items[i].onkeyup = keyboardEventHandler;
}
function keyboardEventHandler(e){ document.getElementById("output").innerHTML = "Character: " + e.keyCode + " Key pressed is:" + String.fromCharCode(e.keyCode);
}
</script>
</html>
Output :- Mouse Events
 
 
Keyboard Events:
 
LAB EXPERIMENT-4
WEEK-4: Create and save an XML document on the server, which contains 10 users information. Write a Java / JavaScript program, which takes User Id as an input and returns the user details by taking the user information from the XML document.
Program:-
Total no. of html files :-01 Total no. of xml files :- 01 html code for user :-
<html>
<head>
<script language="javascript"> function fncDisplayInfo()
{
var xhttp=null; var flag=0;
var userid = document.frm.uname.value;
var xmlDoc = new ActiveXObject("microsoft.xmldom"); xmlDoc.load("user.xml");
var noOfUsers = xmlDoc.getElementsByTagName("userlist")[0].childNodes.length; for(var i=0;i<parseInt(noOfUsers);i++)
{
var uid=xmlDoc.getElementsByTagName("user")[i].childNodes[0].childNodes[0].nodeValue; if(uid == userid)
{
document.write("<h1> User Details</h1>"); var userName
=xmlDoc.getElementsByTagName("user")[i].childNodes[1].childNodes[0].nodeValue; var
Address=xmlDoc.getElementsByTagName("user")[i].childNodes[2].childNodes[0].nodeValue; var phone =xmlDoc.getElementsByTagName("user")[i].childNodes[3].childNodes[0].nodeValue; var email=xmlDoc.getElementsByTagName("user")[i].childNodes[4].childNodes[0].nodeValue; document.write("<br><b>User ID :      "+uid) document.write("<br>User Name : "+userName);
document.write("<br>Address :       "+Address); document.write("<br>Phone no :      "+phone); document.write("<br>E - Mail :       "+email); flag =1;
break;
}
}
if(flag==0)
{
alert("InValid User");
}
}
</script>
</head>
<body style="background-color:DodgerBlue;">
<center>
<h1><b>Enter User ID & Phone Number below</h1>
<form name="frm">
User ID : <input type="text" name="uname"><br><br> Phone : <input type="text" name="phone"><br>
<br><input type="button" name="btn" value="Submit" onclick="fncDisplayInfo()">
</form>
</center>
</body>
</html>
Xml code for user :-
<?xml version="1.0"?>
<userlist>
<user>
<userid>1</userid>
<username>Rishikesh</username>
<address>Ashoknagar</address>
<phone>9100097101</phone>
<email>Rishikesh@gmail.com</email>
</user>
<user>
<userid>2</userid>
<username>Bhavani</username>
<address>Ameerpet</address>
<phone>9100097102</phone>
<email>Bhavani@gmail.com</email>
</user>
<user>
<userid>3</userid>
<username>Sravanthi</username>
<address>SR Nagar</address>
<phone>9100097103</phone>
<email>Sravanthi@yahoo.com</email>
</user>
<user>
<userid>4</userid>
<username>Shiva Ram</username>
<address>Abids</address>
<phone>9100097104</phone>
<email>Shiva@yahoo.com</email>
</user>
<user>
<userid>5</userid>
<username>Bharath</username>
<address>VidhyaNagar</address>
<phone>9100097105</phone>
<email>Bharath@yahoo.com</email>
</user>
<user>
<userid>6</userid>
<username>Rutwik</username>
<address>KOTI</address>
<phone>9100097106</phone>
<email>Rutwik@yahoo.com</email>
</user>
<user>
<userid>7</userid>
<username>Samridhi</username>
<address>Uppal</address>
<phone>9100097107</phone>
<email>Samridhi@yahoo.com</email>
</user>
<user>
<userid>8</userid>
<username>Prathik</username>
<address>Tarnaka</address>
<phone>9100097108</phone>
<email>Prathik@yahoo.com</email>
</user>
<user>
<userid>9</userid>
<username>Ravindra</username>
<address>LB Nagar</address>
<phone>9100097109</phone>
<email>Ravindra@yahoo.com</email>
</user>
<user>
<userid>10</userid>
<username>Mahananda</username>
<address>Malakpet</address>
<phone>9100097110</phone>
<email>Mahananda@yahoo.com</email>
</user>
</userlist>
Output :-  
 
    
 
Editor is loading...
Leave a Comment