Skip to content

Instantly share code, notes, and snippets.

@max-programming
Last active March 21, 2023 09:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-programming/03aeb57468f0aee8dffb077da8c77382 to your computer and use it in GitHub Desktop.
Save max-programming/03aeb57468f0aee8dffb077da8c77382 to your computer and use it in GitHub Desktop.
My Practical Files for Java
import java.util.Scanner;
class Trip {
String source, destination;
String startDate;
int noOfDays;
Trip(String s, String d, String sd, int nod) {
source = s;
destination = d;
startDate = sd;
noOfDays = nod;
}
static Trip bookNow(String s, String d, String sd, int nod) {
Trip trip = new Trip(s, d, sd, nod);
return trip;
}
void showDetail() {
System.out.println("From: " + source);
System.out.println("To: " + destination);
System.out.println("Start date: " + startDate);
System.out.println("Number of days: " + noOfDays);
}
}
public class Practical10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter source: ");
String from = sc.nextLine();
System.out.print("Enter destination: ");
String to = sc.nextLine();
System.out.print("Enter start date: ");
String date = sc.nextLine();
System.out.print("Enter number of days: ");
int noOfDays = sc.nextInt();
Trip trip = Trip.bookNow(from, to, date, noOfDays);
trip.showDetail();
}
}
class Address {
String city;
int pin;
void displayAddress() {
System.out.println("City: " + city);
System.out.println("Pin: " + pin);
}
}
class Biodata extends Address {
String name, qualification, dateOfBirth;
Biodata(String n, String q, String dob) {
name = n;
qualification = q;
dateOfBirth = dob;
}
void display() {
System.out.println("Name: " + name);
System.out.println("Qualification: " + qualification);
System.out.println("Date of birth: " + dateOfBirth);
displayAddress();
}
}
public class Practical11 {
public static void main(String[] args) {
Biodata p1 = new Biodata("Usman", "Diploma", "12/7/2005");
p1.city = "Surat";
p1.pin = 395009;
p1.display();
}
}
class Vehicle {
int id, speed;
}
class Bike extends Vehicle {
Bike(int id, int speed) {
super.id = id;
super.speed = speed;
}
void display() {
System.out.println("ID: " + super.id);
System.out.println("Speed: " + super.speed);
}
}
public class Practical11B {
public static void main(String[] args) {
Bike bike = new Bike(70, 88);
bike.display();
}
}
class Student
{
String name;
int roll, examNo;
Student()
{
roll = 4;
name = "Usman";
examNo = 3;
}
}
class Exam extends Student
{
int m1, m2, m3;
}
class Result extends Exam
{
double total, percent;
Result(int m1, int m2, int m3)
{
super.m1 = m1;
super.m2 = m2;
super.m3 = m3;
}
void calculate()
{
total = super.m1 + super.m2 + super.m3;
percent = (total / 300) * 100;
}
}
public class Practical12
{
public static void main(String[] args)
{
Result result = new Result(90, 80, 10);
result.calculate();
System.out.println("Name: " + result.name);
System.out.println("Roll: " + result.roll);
System.out.println("Exam no: " + result.examNo);
System.out.println("Total: " + result.total);
System.out.println("Percent: " + result.percent + "%");
}
}
class Grandfather
{
String surname, nationality;
}
class Father extends Grandfather
{
String name;
}
class Son extends Father
{
Son()
{
name = "Usman";
surname = "Sabuwala";
nationality = "Indian";
}
}
public class Practical12B
{
public static void main(String[] args)
{
Son s1 = new Son();
System.out.println("Name: " + s1.name);
System.out.println("Surname: " + s1.surname);
System.out.println("Nationality: " + s1.nationality);
}
}
interface PrintData
{
void print();
}
class PrintInt implements PrintData
{
public void print()
{
System.out.println("Print Int " + 10);
}
}
class PrintFloat implements PrintData
{
public void print()
{
System.out.println("Print Float " + 10.55);
}
}
class PrintChar implements PrintData
{
public void print()
{
System.out.println("Print Char " + 'f');
}
}
class Practical13
{
public static void main(String[] args)
{
PrintInt pi = new PrintInt();
PrintFloat pf = new PrintFloat();
PrintChar pc = new PrintChar();
pi.print();
pf.print();
pc.print();
}
}
interface Animal
{
void food();
}
class Dog implements Animal
{
public void food()
{
System.out.println("Dog");
}
}
class Lion implements Animal
{
public void food()
{
System.out.println("Lion");
}
}
public class Practical13B
{
public static void main(String[] args)
{
Dog d = new Dog();
Lion l = new Lion();
d.food();
l.food();
}
}
abstract class Shape
{
public void area()
{
System.out.println("Area for a shape");
}
}
class Triangle extends Shape
{
double base, height;
Triangle()
{
base = 10;
height = 50;
}
public void area()
{
super.area();
System.out.println("Triangle: " + (double) (.5 * base * height));
}
}
class Rectangle extends Shape
{
double width, height;
Rectangle()
{
width = 10;
height = 10;
}
public void area()
{
super.area();
System.out.println("Rectangle: " + (double) (width * height));
}
}
class Circle extends Shape
{
double radius;
Circle()
{
radius = 20;
}
public void area()
{
super.area();
System.out.println("Circle: " + (double) (Math.PI * radius * radius));
}
}
public class Practical14
{
public static void main(String[] args)
{
Triangle t = new Triangle();
Rectangle r = new Rectangle();
Circle c = new Circle();
t.area();
r.area();
c.area();
}
}
abstract class Product
{
int product_id;
abstract void ShowPrice();
abstract void ShowBrand();
}
class MobilePhone extends Product
{
String brand;
void ShowBrand()
{
System.out.println("Brand: " + brand);
}
void ShowPrice()
{
}
}
class Iphone extends MobilePhone
{
int price;
void ShowPrice()
{
System.out.println("Price: " + price);
}
}
public class Practical15
{
public static void main(String[] args)
{
Iphone iphone = new Iphone();
iphone.product_id = 1;
iphone.brand = "Apple";
iphone.price = 999;
if (iphone.product_id == 1)
{
iphone.ShowPrice();
iphone.ShowBrand();
}
}
}
import java.util.Scanner;
class Practical17
{
public static void main(String[] args)
{
// a();
b();
}
static void b()
{
try
{
Scanner sc = new Scanner(System.in);
int pnLength = sc.nextLine().length();
if (pnLength != 10)
throw new Exception("Phone number should only be 10 characters long");
} catch (Exception e)
{
System.out.println(e.getMessage());
}
}
private static void a()
{
try
{
int res = 10 / 0;
} catch (ArithmeticException e)
{
System.out.println("Number cannot be divided by 0");
}
}
}
class Practical3 {
static void a() {
int num = 10;
if (num > 0) {
System.out.println("Positive number");
} else {
System.out.println("Negative number");
}
}
static void b() {
int num = 10;
if (num % 3 == 0) {
System.out.println("Number is divisible by 3");
} else {
System.out.println("Number is not divisible by 3");
}
}
static void c() {
int year = 1976;
if ((year % 4 == 0 && year % 100 != 0) ||
(year % 100 == 0 && year % 400 == 0)) {
System.out.println("It is a leap year!");
} else {
System.out.println("It is not a leap year!");
}
}
static void d() {
int age1 = 19;
int age2 = 44;
int age3 = 18;
int oldest = age1; // assume the oldest is the first person
int youngest = age1; // assume the youngest is the first person
// determine the oldest person
if (age2 > oldest) {
oldest = age2;
}
if (age3 > oldest) {
oldest = age3;
}
// determine the youngest person
if (age2 < youngest) {
youngest = age2;
}
if (age3 < youngest) {
youngest = age3;
}
System.out.println("The oldest person is " + oldest + " years old.");
System.out.println("The youngest person is " + youngest + " years old.");
// if (age1 > age2) {
// if (age1 > age3) {
// System.out.println("Age 1 is oldest");
// } else {
// System.out.println("Age 3 is oldest");
// }
// if (age2 > age3) {
// System.out.println("Age 3 is youngest");
// } else {
// System.out.println("Age 2 is youngest");
// }
// } else {
// if (age2 > age3) {
// System.out.println("Age 2 is oldest");
// } else {
// System.out.println("Age 3 is oldest");
// }
// if (age2 > age3) {
// System.out.println("Age 3 is youngest");
// } else {
// System.out.println("Age 2 is youngest");
// }
// }
}
public static void main(String args[]) {
a();
b();
c();
d();
}
}
import java.util.*;
class Practical3Practice {
static void one(Scanner sc) {
System.out.println("Enter length");
int length = sc.nextInt();
System.out.println("Enter breadth");
int breadth = sc.nextInt();
if (length == breadth) {
System.out.println("It is a square");
} else {
System.out.println("It is a rectangle");
}
}
static void two(Scanner sc) {
System.out.println("Enter your salary");
double salary = sc.nextDouble();
System.out.println("Enter your years of experience");
double years = sc.nextDouble();
if (years > 5) {
salary += salary * 10 / 100;
} else if (years > 2) {
salary += salary * 5 / 100;
} else {
salary += salary * 2 / 100;
}
System.out.println("Your salary with bonus is " + salary);
}
static void three(Scanner sc) {
System.out.println("Enter number of classes held");
double held = sc.nextDouble();
System.out.println("Enter number of classes attended");
double attended = sc.nextDouble();
double presentPer = attended / held * 100;
System.out.println("Your present percentage is " + presentPer + "%");
if (presentPer < 75) {
System.out.println("You are not allowed to sit in exams");
} else {
System.out.println("You are allowed to sit in exams");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
one(sc);
two(sc);
three(sc);
}
}
import java.util.Scanner;
class Practical5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// a(sc);
// b(sc);
// c();
d(sc);
}
private static void d(Scanner sc) {
int num = sc.nextInt();
int c = 0;
for (int i = 0; i <= num; i++) {
if (num % i == 0) {
c++;
}
}
if (c == 2)
System.out.println("It is a prime number");
else
System.out.println("It is not a prime number");
}
private static void c() {
for (int i = 100; i <= 1000; i++) {
if (i % 5 == 0) {
System.out.println(i);
}
}
}
private static void b(Scanner sc) {
System.out.print("Enter a number: ");
String number = Integer.toString(sc.nextInt());
int i = 0;
while (i < number.length()) {
System.out.println(number.charAt(i));
i++;
}
}
private static void a(Scanner sc) {
System.out.print("Enter a number: ");
int number = sc.nextInt();
System.out.print("Press 1 for ascending, press 2 for descending: ");
int choice = sc.nextInt();
if (choice == 1) {
for (int i = 1; i <= number; i++) {
System.out.println(i);
}
} else if (choice == 2) {
for (int i = number; i >= 1; i--) {
System.out.println(i);
}
}
}
}
import java.util.Arrays;
import java.util.Scanner;
class Practical7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// a(sc);
// b(sc);
// c(sc);
d(sc);
}
private static void d(Scanner sc) {
System.out.print("Enter some string: ");
char[] str = sc.nextLine().toCharArray();
Arrays.sort(str);
System.out.println("Sorted string is " + new String(str));
}
private static void c(Scanner sc) {
System.out.print("Enter some string: ");
String str = sc.nextLine().toLowerCase();
String reverse = "";
for (int i = str.length() - 1; i >= 0; i--) {
reverse += str.charAt(i);
}
if (reverse.equals(str))
System.out.println("Provided string is a palindrome");
else
System.out.println("Provided string is not a palindrome");
}
private static void b(Scanner sc) {
System.out.print("Enter some string: ");
String str = sc.nextLine().toLowerCase();
int count = 0;
for (int i = 0; i < str.length(); i++) {
char letter = str.charAt(i);
if (letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u')
count++;
}
System.out.println("The string contains " + count + " vowels");
}
private static void a(Scanner sc) {
System.out.print("Enter some string: ");
String str = sc.nextLine();
System.out.println("Length of string is " + str.length());
}
}
class Person {
int personId, age;
String name;
Person(int pid, int a, String n) {
personId = pid;
age = a;
name = n;
}
void ageGroup() {
if (age < 8)
System.out.println("Child");
else if (age > 8 && age < 20)
System.out.println("Teenage");
else if (age >= 20 && age <= 40)
System.out.println("Young");
else
System.out.println("Old");
}
}
class Student {
int studentId;
String subjectCode;
double marks;
void getData(int sid, String subCode, double m) {
studentId = sid;
subjectCode = subCode;
marks = m;
}
void putData() {
System.out.println("Student id: " + studentId);
System.out.println("Subject code: " + subjectCode);
System.out.println("Marks: " + marks);
}
}
public class Practical8 {
public static void main(String[] args) {
// a();
b();
}
private static void b() {
Person p1 = new Person(1, 90, "Usman");
p1.ageGroup();
}
private static void a() {
Student s1 = new Student();
s1.getData(1, "123", 77.5);
Student s2 = new Student();
s2.getData(024, "456", 88.9);
s1.putData();
s2.putData();
}
}
class Box {
double width, height, depth;
Box() {
width = 10;
height = 10;
depth = 10;
}
Box(int w, int h, int d) {
width = w;
height = h;
depth = d;
}
double volume() {
return width * height * depth;
}
}
public class Practical9 {
public static void main(String[] args) {
Box b1 = new Box();
Box b2 = new Box(17, 20, 21);
System.out.println("Box 1 volume is " + b1.volume());
System.out.println("Box 2 volume is " + b2.volume());
}
}
import java.util.Scanner;
class UsmanCIE1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
one(sc);
two(sc);
three(sc);
}
static void one(Scanner sc) {
System.out.println("Program 1");
System.out.print("Enter a number: ");
int n = sc.nextInt();
if (n < 0)
System.out.println(n + " is negative");
else
System.out.println(n + " is zero or positive");
}
static void two(Scanner sc) {
System.out.println("\nProgram 2");
System.out.print("Enter your choice (1 for max. of two numbers. 2 for sum of two numbers): ");
int choice = sc.nextInt();
System.out.print("Enter first number: ");
int n1 = sc.nextInt();
System.out.print("Enter second number: ");
int n2 = sc.nextInt();
switch (choice) {
case 1:
System.out.println("Maximum of both numbers is " + Math.max(n1, n2));
break;
case 2:
System.out.println("Sum of both numbers is " + (n1 + n2));
break;
default:
System.out.println("Invalid choice");
break;
}
}
static void three(Scanner sc) {
System.out.println("\nProgram 3");
int count = 0;
System.out.print("Enter a number: ");
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
if (n % i == 0)
count++;
}
if (n == 1 || count == 2)
System.out.println(n + " is a prime number");
else
System.out.println(n + " is not a prime number");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment