[go: up one dir, main page]

0% found this document useful (0 votes)
22 views9 pages

Week 1

Uploaded by

billionoid
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)
22 views9 pages

Week 1

Uploaded by

billionoid
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
You are on page 1/ 9

Date: SheetNo:

WEEK1
1.Create a class Hello which can contains all the primitive datatypes:
byte, int, short, long, float, double, char,
a. print all the default values
b. set the values to them and print those values
program:
class Hello {
byte byteValue;
short shortValue;
int intValue;
long longValue;
float floatValue;
double doubleValue;
char charValue;
public void printDefaultValues() {
System.out.println("Default values:");
System.out.println("byte: " + byteValue);
System.out.println("short: " + shortValue);
System.out.println("int: " + intValue);
System.out.println("long: " + longValue);
System.out.println("float: " + floatValue);
System.out.println("double: " + doubleValue);
System.out.println("char: '" + charValue + "'");
}
public void setValues() {
byteValue = 10;
shortValue = 20;
intValue = 30;
longValue = 40000L;
Date: SheetNo:
floatValue = 5.5f;
doubleValue = 10.10;
charValue = 'A';
System.out.println("Updated values:");
System.out.println("byte: " + byteValue);
System.out.println("short: " + shortValue);
System.out.println("int: " + intValue);
System.out.println("long: " + longValue);
System.out.println("float: " + floatValue);
System.out.println("double: " + doubleValue);
System.out.println("char: '" + charValue + "'");
}
public static void main(String[] args) {
Hello hello = new Hello();
hello.printDefaultValues();
hello.setValues();
}
}
Date: SheetNo:

2.Write a java application to read two numbers and print the sum of
the two numbers
a. By assigning values directly to the variables
b. By using the Scanner class for user input
Program:
import java.util.Scanner;
public class SumCalculator {
public static void main(String[] args) {
int num1 = 5; // Direct assignment
int num2 = 10; // Direct assignment
int sum = num1 + num2;
System.out.println("Sum of " + num1 + " and " + num2 + " (direct assignment): " +
sum);
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int userNum1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int userNum2 = scanner.nextInt();
int userSum = userNum1 + userNum2;
System.out.println("Sum of " + userNum1 + " and " + userNum2 + " (user input): " +
userSum);
scanner.close();
}
}
Date: SheetNo:

3.Create a class called Student with the Data members int sno,
m1,m2,m3,m3,total
float avg,
Member Functions setStudentDetails computeAverage
displayStudentDetails
Program:
public class Student {
private int sno;
private int m1, m2, m3;
private int total;
private float avg;
public void setStudentDetails(int sno, int m1, int m2, int m3) {
this.sno = sno;
this.m1 = m1;
this.m2 = m2;
this.m3 = m3;
computeAverage();
}
private void computeAverage() {
total = m1 + m2 + m3; // Calculate total
avg = total / 3.0f; // Calculate average
}
public void displayStudentDetails() {
System.out.println("Student Number: " + sno);
System.out.println("Marks in Subject 1: " + m1);
System.out.println("Marks in Subject 2: " + m2);
System.out.println("Marks in Subject 3: " + m3);
System.out.println("Total Marks: " + total);
Date: SheetNo:
System.out.println("Average Marks: " + avg);
}

public static void main(String[] args) {


Student student = new Student();
student.setStudentDetails(1, 85, 90, 80);
student.displayStudentDetails();
}
}
Date: SheetNo:

4.Create a class Vehicle which contains the data members vno, vname,
company, typefuel member functions insertDetails)
getDetails)
program:
public class Vehicle {
private String vno;
private String vname;
private String company;
private String typeFuel;
public void insertDetails(String vno, String vname, String company, String typeFuel) {
this.vno = vno;
this.vname = vname;
this.company = company;
this.typeFuel = typeFuel;
}
public void getDetails() {
System.out.println("Vehicle Number: " + vno);
System.out.println("Vehicle Name: " + vname);
System.out.println("Company: " + company);
System.out.println("Type of Fuel: " + typeFuel);
}
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
vehicle.insertDetails("AB1234", "Model X", "Tesla", "Electric");
vehicle.getDetails();
}
}
Date: SheetNo:

5.Create a class employee with the data members int eno, String
ename float esal
Member Functions
setEmployee - to set the values to the employee displayEmployee - to
display the values
program:
class Employee {
private int eno;
private String ename;
private float esal;
public void setEmployee(int eno, String ename, float esal) {
this.eno = eno;
this.ename = ename;
this.esal = esal;
}
public void displayEmployee() {
System.out.println("Employee Number: " + eno);
System.out.println("Employee Name: " + ename);
System.out.println("Employee Salary: " + esal);
}
public static void main(String[] args) {
Employee emp = new Employee();
emp.setEmployee(1, "John Doe", 50000.0f);
emp.displayEmployee();
}
}
Date: SheetNo:

6.Write a class called Bank with the data members acno, actype, name,
bal,
Member functions
Insert CustomerDetails- method to insert the values to the variables
Deposit Amount-ask the user to enter the amount to deposit and add
the amount entered to the bal
Withdraw Amount - ask the user to enter the amount to withdraw and
update the amount entered to the b.
Program:
import java.util.Scanner;
class Bank {
private int acno;
private String actype;
private String name;
private float bal;
public void insertCustomerDetails(int acno, String actype, String name, float bal) {
this.acno = acno;
this.actype = actype;
this.name = name;
this.bal = bal;
}
public void depositAmount() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter amount to deposit: ");
float amount = scanner.nextFloat();
bal += amount;
System.out.println("Amount deposited successfully. New balance: " + bal);
}
public void withdrawAmount() {
Date: SheetNo:
Scanner scanner = new Scanner(System.in);
System.out.print("Enter amount to withdraw: ");
float amount = scanner.nextFloat();
if (amount <= bal) {
bal -= amount;
System.out.println("Amount withdrawn successfully. New balance: " + bal);
} else {
System.out.println("Insufficient balance.");
}
}
public void displayAccountDetails() {
System.out.println("Account Number: " + acno);
System.out.println("Account Type: " + actype);
System.out.println("Customer Name: " + name);
System.out.println("Balance: " + bal);
}
public static void main(String[] args) {
Bank bankAccount = new Bank();
bankAccount.insertCustomerDetails(101, "Savings", "Alice", 1000.0f);
bankAccount.displayAccountDetails();
bankAccount.depositAmount();
bankAccount.withdrawAmount();
bankAccount.displayAccountDetails();
}
}

You might also like