Skip to main content

Practical Paper solution B

Q-1 A>

Java program that prompts the user to input the base and height of a triangle. Accordingly calculates and displays the area of a triangle using the formula (base* height) / 2, and handles any input errors such as non-numeric inputs or negative values for base or height. Additionally, include error messages for invalid input and provide the user with the option to input another set of values or exit the program


Source Code :-

import java.util.InputMismatchException;
import java.util.Scanner;

class q1_a
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
boolean continueProgram = true;

while(continueProgram)
{
try{
System.out.print("Enter the base of the traingle : ");
double base = s.nextDouble();

System.out.print("Enter the height of traingle : ");
double height = s.nextDouble();

if(base <= 0 || height <= 0)
{
throw new IllegalArgumentException("Base and height must be positive number.");

}
double area = (base*height)/2;
System.out.println("Area of the traingle : " +area);
System.out.println("Do you want ti calculate the area for another traingle?(yes,no) : ");
String choice = s.next().toLowerCase();

if(!choice.equals("yes"))
{
continueProgram = false;
}
}
catch(InputMismatchException e)
{
System.out.println(e.getMessage());
}
catch(IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
}
}

}


Q-2 B >

Create a Java applet that allows the user to interactively change the background color by clicking on a button. The background color of the applet should change to a random color. Keep track of the number of times the button is clicked and display it on the applet. Include a "Reset" button that resets the counter and changes the background color back to the default color (e.g., white).


Source Code :-

import java.applet.*;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class q1_b extends Applet implements ActionListener {
    private Button changeColorButton;
    private Button resetButton;
    private int clickCount = 0;
    Random random = new Random();

    public void init() {
        changeColorButton = new Button("Change Color");
        resetButton = new Button("Reset");
        add(changeColorButton);
        add(resetButton);

        changeColorButton.addActionListener(this);
        resetButton.addActionListener(this);
    }

    public void paint(Graphics g) {
        setBackground(Color.WHITE);

        g.drawString("Click Count: " + clickCount, 50, 50);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == changeColorButton) {
            clickCount++;

            Color randomColor = new Color(random.nextInt(256),random.nextInt(256) ,random.nextInt(256) );

            setBackground(randomColor);
    repaint();
        } else if (e.getSource() == resetButton) {
            clickCount = 0;

            setBackground(Color.WHITE);
    repaint();
        }
    }
}

/*
<applet code="q1_b.class" width="300" height="200"></applet>
*/

Comments

Popular posts from this blog

C++ programming language

                c++ is a general programming language and is widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features.C++ runs on lots of platforms like Windows, Linux, Unix, Mac etc.   ❖❖  C++ with oops ❖❖

Pratical -206 paper solution -3

Full course of c++ programming language Click here > C++ programming language Q-1 [A] Write a c program to create structure of employee with members Empid, EmpName, Qualification and EmpSalary by taking input of 5 employees display the employee whose qualification is "MBA" and salary greater than 20000 . #include <stdio.h> #include <conio.h> #include <string.h> struct Employee {     int Empid, EmpSalary;     char EmpName[50];     char Qualification[50]; }; Void main() {     struct Employee emp[5];     int i;     for (i = 0; i < 5; i++)     {         printf("Enter employee ID: ");         scanf("%d", &emp[i].Empid);         printf("Enter employee name: ");         scanf("%s", emp[i].EmpName);         printf("Enter employee qualification: ");         scanf("%s", emp[i].Quali...

9. Jumps Statement in c++

➢ Jumps in Loops:- ➢ C++ break and continue Statement:-                     There are two statements (break; and continue ;) built in C++ programming to alter the normal flow of program.                     Loops are used to perform repetitive task until test expression is false but sometimes it is desirable to skip some statement/s inside loop or terminate the loop immediately with checking test condition. On these type of scenarios, continue; statement and break; statement is used respectively. The break; statement is also used to terminate switch statement. 1) break Statement :-                       The break; statement terminates the loop(for, while and do..while loop) and switch statement immediately when it appears . Syntax of break :- break;              ...