0% found this document useful (0 votes)
5 views15 pages

Java

The document contains various programming examples in Java, HTML, CSS, and JavaScript, covering topics such as classes and objects, method and constructor overloading, inheritance, exception handling, and building static websites. It also includes a stopwatch application, email validation, and a React application demonstrating hooks and routing. Each section provides code snippets and explanations for implementing the respective functionalities.

Uploaded by

ninadveer01
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)
5 views15 pages

Java

The document contains various programming examples in Java, HTML, CSS, and JavaScript, covering topics such as classes and objects, method and constructor overloading, inheritance, exception handling, and building static websites. It also includes a stopwatch application, email validation, and a React application demonstrating hooks and routing. Each section provides code snippets and explanations for implementing the respective functionalities.

Uploaded by

ninadveer01
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

1. Programs on classes and objects.

class student {
String name;
int age;

void start(){
[Link]("name:" + name);
[Link]("age:" + age);
}
}
public class Main {
public static void main(String[]args)
{
student s1 = new student();
[Link] = "Atharva";
[Link] = 19;
[Link]();
}
}

2. Programs on method and constructor overloading


I. Method overloading

class Calculator {
int add(int a, int b) {
return a + b;
}

int add(int a, int b, int c) {


return a + b + c;

double add(double a, double b) {


return a + b;
}
}

public class Main {


public static void main(String[] args) {
Calculator calc = new Calculator();
[Link]("Sum(int, int): " + [Link](10, 10));
[Link]("Sum(int, int, int): " + [Link](10, 20, 30));
[Link]("Sum(double, double): " + [Link](1.7, 1.5));
}
}
II. Constructor Overloading

class Student {
String name;
int age;

Student() {
name = "Unknown";
age = 0;
}

Student(String n) {
name = n;
age = 19;
}

Student(String n, int a) {
name = n;
age = a;
}

void display() {
[Link]("Name: " + name + " Age: " + age);
}
}

public class Main {


public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student("Atharva");
Student s3 = new Student("Prit", 19);
[Link]();
[Link]();
[Link]();
}
}

3. Program on various types of inheritance and Exception


handling
I. Single Inheritance

class Animal {
void eat() {
[Link]("Animal eats");
}
}
class Dog extends Animal {
void sound() {
[Link]("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}

II. Multilevel Inheritance

class Animal {

void eat() {
[Link]("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog is barking");
}
}

class Puppy extends Dog {


void weep() {
[Link]("Puppy is weeping");
}
}
public class Main {
public static void main(String[] args) {
Puppy puppy = new Puppy();
[Link]();
[Link]();
[Link]();
}
}

III. Hierarchical inheritance

class Animal {
void eat() {
[Link]("Animal eats");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks.");
}
}
class Cat extends Animal {
void meow() {
[Link]("Cat meows.");

public class Main {


public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();

Cat c = new Cat();


[Link]();
[Link]();
}
}

IV. Try-Catch Block


public class Main {
public static void main(String[] args) {
try {
int a = 10, b = 0;
int result = a / b; // ArithmeticException
[Link]("Result: " + result);
} catch (ArithmeticException e) {
[Link]("Exception caught: " + e);
} finally {
[Link]("This block always executes");
}
}
}/

V. User defined exception


class MyException extends Exception {
MyException(String message) {
super(message);
}
}
public class Main {
public static void main(String[] args) {
try {
int age = 15;
if (age < 18) {
throw new MyException("Age must be 18 or above");
}
} catch (MyException e) {
[Link]("Custom Exception: " + [Link]());
}
}
}
5. Program to create a static website using HTML and CSS
HTML: [Link]

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>My Static Website</title>
<link rel="stylesheet" href="[Link]" />
</head>
<body>
<header>
<h1>Welcome to My Static Website</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<main>
<section>
<h2>Hello, Dear Student!</h2>
<p>This is a sample static website built using HTML, CSS, and
JavaScript.</p>
<button onclick="showMessage()">Click Me</button>
<p id="message"></p>
</section>
</main>
<footer>
<p>© 2025 FullStackJavaSyllusSetter</p>
</footer>

<script src="[Link]"></script>
</body>
</html>

[Link]

body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f9f9f9;
color: #333;
}

header {
background-color: #007acc;
color: white;
padding: 20px;
text-align: center;
}

nav ul {
list-style: none;
padding: 0;
display: flex;
justify-content: center;
margin-top: 10px;
}

nav li {
margin: 0 15px;
}

nav a {
color: white;
text-decoration: none;
font-weight: bold;
}

main {
padding: 20px;
text-align: center;
}

button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}

footer {
background-color: #eee;
text-align: center;
padding: 15px;
margin-top: 40px;
}
Java Script: [Link]

function showMessage() {
const message = [Link]("message");
[Link] = "Thank you for clicking! Enjoy browsing ";
}

6. Program using Java Script to validate the email address


entered by the user (check the presence of “@” & “.”
character. If this character is missing, the script should
display an alert box reporting the error and ask the user to
re-enter it again).
HTML: [Link]

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>My Static Website</title>
<link rel="stylesheet" href="[Link]" />
</head>
<body>
<header>
<h1>Welcome to My Static Website</h1>
<nav>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>

</ul>
</nav>
</header>

<main>
<section>
<h2>Hello, Dear Students!</h2>
<p>This is a sample static website built using HTML, CSS, and
JavaScript.</p>
<button onclick="showMessage()">Click Me</button>
<p id="message"></p>

<h3>Subscribe to our Newsletter</h3>


<input type="text" id="email" placeholder="Enter your email" />
<button onclick="validateEmail()">Submit</button>
</section>
</main>

<footer>
<p>© 2025 FullStackJavaSyllabusSetter</p>
</footer>

<script src="[Link]"></script>
</body>
</html>

[Link]

body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f9f9f9;
color: #333;
}

header {
background-color: #007acc;
color: white;
padding: 20px;
text-align: center;
}

nav ul {
list-style: none;
padding: 0;
display: flex;
justify-content: center;
margin-top: 10px;
}

nav li {
margin: 0 15px;

nav a {
color: white;
text-decoration: none;
font-weight: bold;
}

main {
padding: 20px;
text-align: center;
}

button {
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}

footer {
background-color: #eee;
text-align: center;
padding: 15px;
margin-top: 40px;
}

Java Script: [Link]

function showMessage() {
const message = [Link]("message");
[Link] = "Thank you for clicking! Enjoy browsing �";
}

function validateEmail() {
const emailInput = [Link]("email");
const email = [Link];

if ([Link]("@") === -1 || [Link](".") === -1) {


alert("Invalid email address. Please include '@' and '.' characters.");
[Link]();
return false;
}
alert("Email is valid!");
return true;
}

7. Program to Build a Stop Watch using Java Script


HTML File: [Link]

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Stopwatch</title>
<link rel="stylesheet" href="[Link]">
<link rel="preconnect" href="[Link]
<link rel="preconnect" href="[Link] crossorigin>
<link
href="[Link]
rel="stylesheet">
</head>
<body>
<div id="timer">00:00:00</div>
<div id="buttons">
<button id="start">Start</button>
<button id="stop">Stop</button>
<button id="reset">Reset</button>
</div>
<script src="[Link]"></script>
</body>
</html>

CSS File:[Link]

body {
background: #360033; /* fallback for old browsers */
background: -webkit-linear-gradient(to right, #0b8793, #360033);
background: linear-gradient(to right, #0b8793, #360033); /* W3C, IE 10+/ Edge, Firefox
16+,
Chrome 26+, Opera 12+, Safari 7+ */
font-family: 'Nunito', sans-serif;
display: flex;
flex-direction: column;
justify-content: center;
min-height: 100vh;
overflow: hidden;
align-items: center;
}
#timer{
font-size: 7rem;
font-weight: 600;
text-shadow: 2px 2px #5f5f56;
color: #F1F2B5;
width: 500px;
text-align: center;
margin: 40px auto;
}

#buttons{
display: flex;
justify-content: center;
}
button{
font-family: 'Nunito', sans-serif;
background-color: #F1F2B5;
color: #0b8793;
border: none;
font-size: 2rem;
font-weight: bold;
padding: 1.5rem 4rem;
margin: 1rem;
border-radius: 15px;
cursor: pointer;
box-shadow: 2px 2px 10px rgba(58, 51, 51, 0.3);
transition: all .2s;
}
button:hover{
background-color: #2c3e50;
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.5);
}
button[disabled]{
opacity: .5;
cursor: default;
}
@media (max-width: 800px) {
#timer{
font-size: 4rem;
width: 350px;
}
button {
font-size: calc(20px + (30 - 20) * ((100vw - 320px) / (1200 - 320)));
padding: calc(20px + (50 - 20) * ((100vw - 320px) / (1200 - 320)));
}
}

Java Script File: [Link]

const timer = [Link]('timer');


const startButton = [Link]('start');
const stopButton = [Link]('stop');
const resetButton = [Link]('reset');

let startTime = 0;
let elapsedTime = 0;
let timerInterval;
function startTimer(){
startTime = [Link]() - elapsedTime

timerInterval = setInterval( ()=> {


elapsedTime = [Link]() - startTime
[Link] = formatTimer(elapsedTime);
}, 10)

[Link] = true;
[Link] = false;
}

function stopTimer(){
clearInterval(timerInterval);
[Link] = false;
[Link] = true;
}

function resetTimer(){
clearInterval(timerInterval);

elapsedTime = 0;
[Link] = "00:00:00";

[Link] = false;
[Link] = false;
}

function formatTimer(elapsedTime){
const hours = [Link](elapsedTime / (1000 * 60 * 60));
const minutes = [Link]((elapsedTime % (1000 * 60 * 60)) / (1000 * 60))
const seconds = [Link]((elapsedTime % (1000 * 60)) / 1000);
const mseconds = [Link]((elapsedTime % 1000) / 10);
return (
(hours ? (hours > 9 ? hours : "0" + hours) : "00")
+ ":" +
(minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00")
+ ":" +
(seconds ? (seconds > 9 ? seconds : "0" + seconds) : "00")
+ "." +
(mseconds > 9 ? mseconds : "0" + mseconds));
}
[Link]('click', startTimer)

[Link]('click', stopTimer)
[Link]('click', resetTimer)

8. Program for making use of React Hooks that displays four


buttons namely, “Red”, “Blue”,“Green”, “Yellow”. On
clicking any of these buttons, the code displays the message
that you /have selected that particular color.

Code ([Link])

import React, { useState } from 'react';


function App() {
const [selectedColor, setSelectedColor] = useState('');
const handleColorClick = (color) => {
setSelectedColor(color);
};
return (
<div style={{ textAlign: 'center', marginTop: '50px' }}>
<h2>Select a Color</h2>
<div style={{ marginBottom: '20px' }}>
<button onClick={() => handleColorClick('Red')} style={{ margin: '5px',

backgroundColor: 'red', color: 'white', padding: '10px 20px' }}>Red</button>


<button onClick={() => handleColorClick('Blue')} style={{ margin: '5px',
backgroundColor: 'blue', color: 'white', padding: '10px 20px' }}>Blue</button>
<button onClick={() => handleColorClick('Green')} style={{ margin: '5px',
backgroundColor: 'green', color: 'white', padding: '10px 20px' }}>Green</button>
<button onClick={() => handleColorClick('Yellow')} style={{ margin: '5px',
backgroundColor: 'gold', color: 'black', padding: '10px 20px' }}>Yellow</button>
</div>
{selectedColor && (
<h3>You have selected <span style={{ color: [Link]()
}}>{selectedColor}</span>.</h3>
)}
</div>
);}
export default App;

9. Creating a Single Page website using the concepts in React


like Hooks, Router, Props and States.
[Link]

import React from 'react';


import ReactDOM from 'react-dom/client';
import App from './App';
import { BrowserRouter } from 'react-router-dom';

const root = [Link]([Link]('root'));


[Link](
<BrowserRouter>
<App />
</BrowserRouter>
);

[Link]

import React from 'react';


import { Routes, Route } from 'react-router-dom';
import Navbar from './components/Navbar';
import Home from './pages/Home';
import About from './pages/About';
import Contact from './pages/Contact';

function App() {
return (
<>
<Navbar />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact name="React User" />} />
</Routes>
</>
);
}
export default App;

[Link]

import React from 'react';


import { Link } from 'react-router-dom';

function Navbar() {
return (
<nav style={{ padding: '10px', backgroundColor: '#eee' }}>
<Link to="/" style={{ marginRight: '10px' }}>Home</Link>
<Link to="/about" style={{ marginRight: '10px' }}>About</Link>
<Link to="/contact">Contact</Link>
</nav>
);
}
export default Navbar;

[Link]

import React from 'react';


function Home() {
return (
<div style={{ padding: '20px' }}>
<h2>Welcome to the Home Page</h2>
<p>This is a simple SPA built with React Router and Hooks.</p>
</div>
);
}
export default Home;

[Link]

import React from 'react';


function About() {
return (
<div style={{ padding: '20px' }}>
<h2>About Page</h2>
<p>This page demonstrates routing in a React app.</p>
</div>
);
}
export default About;

[Link]

import React, { useState } from 'react';

function Contact({ name }) {


const [message, setMessage] = useState('');

const handleSubmit = (e) => {


[Link]();
alert(`Thanks, ${name}. Message sent: "${message}"`);
setMessage('');
};
return (
<div style={{ padding: '20px' }}>
<h2>Contact Page</h2>
<form onSubmit={handleSubmit}>
<textarea
value={message}
onChange={(e) => setMessage([Link])}
placeholder="Type your message..."
rows="4"
cols="50"
/>
<br />
<button type="submit" style={{ marginTop: '10px' }}>Send Message</button>
</form>
</div>
);
}

export default Contact;

You might also like