Journal Programs (2)
Journal Programs (2)
import java.util.Scanner;
public class q1 {
public static void main(String[] args) {
Scanner aki = new Scanner(System.in);
System.out.print("Enter the number of terms in the Fibonacci series: ");
int n = aki.nextInt();
aki.close();
int firstTerm = 0;
int secondTerm = 1;
if (n >= 1) {
System.out.print(firstTerm);
}
if (n >= 2) {
System.out.print(", " + secondTerm);
}
System.out.println();
}
}
===========================================================
// 2. Write a java program to define the given no. of series is either even or
// odd.
import java.util.Scanner;
public class q2 {
public static void main(String args[]) {
// take input from the user
// create new instance of scanner classs
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number: ");
int num = scan.nextInt();
System.out.println("Given number " + num + " is " +
((num % 2 == 0) ? "even" : "odd"));
}
}
===========================================================
// 3. Write a java program to print all armstrong numbers
import java.util.Scanner;
public class q3 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
===========================================================
// 4. Write a program for entering array elements from use and print the sum of
// given elements and provide average of it.
import java.util.Scanner;
public class q4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
scanner.close();
int sum = 0;
for (int num : arr) {
sum += num;
}
===========================================================
import java.util.Scanner;
public class q5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows for the left arrow pattern: ");
int n = scanner.nextInt();
scanner.close();
===========================================================
import java.util.Scanner;
public class q6 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows for the inverse star pattern: ");
int n = scanner.nextInt();
scanner.close();
===========================================================
class Animal {
void makeSound() { System.out.println("The animal makes a generic sound."); }
}
public class q7 {
public static void main(String[] args) {
Animal animal = new Animal();
Animal dog = new Dog();
Animal cat = new Cat();
animal.makeSound();
dog.makeSound();
cat.makeSound();
}
}
===========================================================
import java.util.Scanner;
public class q8 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int totalMarks = 0;
for (int i = 1; i <= 5; i++) {
System.out.print("Subject " + i + ": ");
int marks = scanner.nextInt();
totalMarks += marks;
}
scanner.close();
===========================================================
class Student {
private String name;
private int age;
public class q9 {
public static void main(String[] args) {
Student student1 = new Student("Ishwari", 20);
Student student2 = new Student("Kashish", 22);
System.out.println("Student 1 Information:");
student1.displayInfo();
System.out.println("\nStudent 2 Information:");
student2.displayInfo();
}
}
===========================================================
import java.util.Scanner;
class Shape {
void display() { System.out.println("This is a shape."); }
}
Circle(double r) { radius = r; }
void display() {
super.display();
System.out.println("This is a circle with radius " + radius);
System.out.println("Area of the circle: " + calculateArea());
}
}
Square(double s) { side = s; }
void display() {
super.display();
System.out.println("This is a square with side length " + side);
System.out.println("Area of the square: " + calculateArea());
}
}
scanner.close();
System.out.println("\nShape Information:");
circle.display();
square.display();
}
}
===========================================================
class Vehicle {
void start() { System.out.println("Vehicle is starting."); }
}
System.out.println("Car:");
car.start();
car.accelerate();
System.out.println("\nMotorcycle:");
motorcycle.start();
motorcycle.wheelie();
}
}
===========================================================
interface Shape {
void draw();
}
interface Color {
void applyColor();
}
class Circle implements Shape, Color {
@Override
public void draw() {
System.out.println("Drawing a circle.");
}
@Override
public void applyColor() {
System.out.println("Applying color to the circle.");
}
}
===========================================================
class Animal {
void sound() { System.out.println("Animals make sounds"); }
}
===========================================================
class Instrument {
void play() { System.out.println("Playing an instrument"); }
}
===========================================================
interface Edible {
void eat();
}
interface Fruit {
String getName();
}
@Override
public String getName() {
return "Apple";
}
}
===========================================================
// 1. String Length
int length = text.length();
System.out.println("1. String Length: " + length);
// 2. Concatenation
String greeting = "Hello";
String name = "Akshay";
String message = greeting + ", " + name + "!";
System.out.println("2. Concatenation: " + message);
// 4. Substring
String sub = text.substring(0, 5);
System.out.println("4. Substring: " + sub);
// 7. Replace
String replaced = text.replace("World", "Java");
System.out.println("7. Replace 'World' with 'Java': " + replaced);
// 8. Split
String sentence = "This is a sample sentence.";
String[] words = sentence.split(" ");
System.out.print("8. Split: ");
for (String word : words) {
System.out.print(word + " ");
}
System.out.println();
}
}
===========================================================
import java.util.Scanner;
===========================================================
import java.util.Scanner;
char grade;
===========================================================
import java.util.Scanner;
===========================================================
import java.util.Scanner;
class Employee {
int id;
String name;
String department;
String post;
double salary;
System.out.print("ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume the newline
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Department: ");
String department = scanner.nextLine();
System.out.print("Post: ");
String post = scanner.nextLine();
System.out.print("Salary: ");
double salary = scanner.nextDouble();
scanner.nextLine(); // Consume the newline
scanner.close();
System.out.println("Employee Information:");
for (int i = 0; i < 10; i++) {
employees[i].displayInfo();
}
}
}
===========================================================
class MathUtility {
public static int add(int a, int b) { return a + b; }
===========================================================
class MyClass {
final int myFinalVariable = 10;
final void myFinalMethod() { System.out.println("This is a final method."); }
}
===========================================================
System.out.println("Colors:");
for (String color : colors) {
System.out.println(color);
}
}
}
===========================================================
// 24. Write a java program for swapping two numbers using third variable
import java.util.Scanner;
System.out.println("Original Numbers:");
System.out.println("First number: " + num1);
System.out.println("Second number: " + num2);
System.out.println("\nSwapped Numbers:");
System.out.println("First number: " + num1);
System.out.println("Second number: " + num2);
}
}
===========================================================
===========================================================