0% found this document useful (0 votes)
2 views12 pages

Assignment 1 Java

The document outlines a series of programming assignments focused on Java, covering topics such as primitive data types, static and instance variables, local variables, type casting, string concatenation, default values, object independence, shared static variables, final variables, and combining multiple concepts. Each assignment includes specific tasks, instructions, and example code to illustrate the concepts being taught. The document serves as a comprehensive guide for students to practice and understand fundamental Java programming principles.

Uploaded by

arivumathi116
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)
2 views12 pages

Assignment 1 Java

The document outlines a series of programming assignments focused on Java, covering topics such as primitive data types, static and instance variables, local variables, type casting, string concatenation, default values, object independence, shared static variables, final variables, and combining multiple concepts. Each assignment includes specific tasks, instructions, and example code to illustrate the concepts being taught. The document serves as a comprehensive guide for students to practice and understand fundamental Java programming principles.

Uploaded by

arivumathi116
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

Assignment 1

1. Display Primitive Data Types

Task:
Create a Java program that demonstrates the use of primitive data types.

Instructions:

1. Create variables of type int, double, char, and boolean.

2. Assign suitable values to each variable.

3. Print each variable using [Link]().

4. Print a descriptive message before each value.

Expected Output (Example):

Age = 21

Salary = 45000.75

Grade = A

Passed = true

CODE:
class exp1{
public static void main(String args[])
{
int age = 21;
double salary = 45000.75;
char grade= 'A';
boolean passed= true;
[Link]("Age =" + age );
[Link]("Salary =" + salary);
[Link]("Grade ="+ grade);
[Link]("Passed ="+ passed);

}
}
OUTPUT:

2. Demonstrate Static and Instance Variables

Task:
Create a class named Student.

Instructions:

1. Create a static variable named schoolName and assign "ABC School".

2. Create an instance variable named rollNo.

3. Create two objects.

4. Assign different roll numbers to each object.

5. Print the school name and roll number of both students.

6. Change the school name using the class name.

7. Print the values again and observe the change.

CODE:
public class exp2 {
static String schoolname="ABC Shool";
int rollno;

public static void main(String args[]){


exp2 a1= new exp2();
exp2 a2 = new exp2();
[Link] = 3;
[Link] = 90;

[Link]("student 1: "+[Link] + "


"+[Link]);
[Link]("student2: "+[Link] +" " +[Link]);
[Link] = "SRIT";
[Link]("student 1: "+[Link] +" "+
[Link]);

[Link]("student2: "+[Link] + " "+


[Link]);

}
}

OUTPUT:

3. Demonstrate Local Variables

Task:
Write a program to understand local variables.

Instructions:

1. Create a class named Demo.

2. Create a method named display().

3. Declare a local variable inside the method.

4. Assign a value to the local variable.

5. Print the local variable.

6. Call the method from main().


CODE:
public class demo {
public void display(){
int a = 10;
[Link](a);
}
public static void main(String args[]){
demo s = new demo();
[Link]();

}
}

OUTPUT:

4. Demonstrate Implicit and Explicit Type Casting

Task:
Write a program to demonstrate type conversion.

Instructions:

1. Create an int variable and assign a value.

2. Store it in a double variable (implicit conversion).

3. Print both values.

4. Create a double variable.

5. Convert it into an int using explicit casting.

6. Print both values.

Expected Concept:

 Widening conversion
 Narrowing conversion

CODE:
public class exp4 {
public static void main(String args[]){
int a = 10;
double b = a;
[Link](a);
[Link](b);
double c= 14.9;
int d = (int)c;
[Link](c);
[Link](d);
}
}

OUTPUT:

5. Demonstrate String Concatenation

Task:
Write a program to understand how the + operator works with strings.

Instructions:

1. Create two integer variables.

2. Print the following:

o "Sum = " + a + b

o "Sum = " + (a + b)

3. Observe the difference in the output.


CODE:
public class exp5 {
public static void main(String[] args) {
int a = 50;
int b = 90;
[Link]("sum=" + a + b);
[Link]("Sum=" + (a + b));
}

OUTPUT:

6. Demonstrate Default Values of Fields

Task:
Write a program to observe default values.

Instructions:

1. Create instance variables of type:

o int

o double

o boolean

o char

o String

2. Do not initialize them.

3. Create an object.
4. Print all the variables.

5. Observe their default values.

CODE:
public class exp6{
int a;
double b;
boolean d;
char c;
String s;
public static void main(String[] args) {
exp6 r = new exp6();
[Link](r.a);
[Link](r.b);
[Link](r.d);
[Link](r.c);
[Link](r.s);
}
}

OUTPUT:

7. Demonstrate Object Independence

Task:
Create two objects and modify one of them.

Instructions:

1. Create a class named Employee.

2. Add an instance variable named salary.

3. Create two objects.


4. Assign different salary values.

5. Change the salary of only the first object.

6. Print the salaries of both objects.

7. Observe that changing one object does not affect the other.

CODE:
public class Employee {
double salary;
public static void main(String[] args) {
Employee emp1 = new Employee();
Employee emp2 = new Employee();
[Link] =40000;
[Link] = 60000;
[Link] = 75000;
[Link]( [Link]);
[Link]( [Link]);
}
}

OUTPUT:

8. Demonstrate Shared Static Variables

Task:
Understand how static variables are shared.

Instructions:

1. Create a class named Counter.

2. Create a static variable named count.

3. Create three objects.

4. Increment count after creating each object.


5. Print the value of count using each object.

6. Print the value using the class name.

CODE:
public class Counter {
static int count;
public static void main(String[] args) {
Counter c1 = new Counter();
count++;
Counter c2 = new Counter();
count++;
Counter c3 = new Counter();
count++;
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
[Link]([Link]);
}

OUTPUT:

9. Demonstrate Final Variables

Task:
Write a program using the final keyword.

Instructions:

1. Declare a final integer variable.

2. Initialize it with a value.


3. Print the value.

4. Try assigning another value to the same variable.

5. Observe and explain the compilation error.

CODE:

OUTPUT:

10. Combine Multiple Concepts

Task:
Create a simple Book class.

Instructions:

1. Create a static variable named libraryName.

2. Create instance variables:

o bookName , price

3. Create a method named display().

4. Inside the method, declare a local variable named discount.

5. Print:

o Library name

o Book name

o Price
o Discount

6. Create two book objects with different details.

7. Change the library name once using the class name.

8. Print the details of both books again.

9. Observe which values changed and which remained different for each object.

CODE:
OUTPUT:

You might also like