0% found this document useful (0 votes)
22 views

Chapter Two Java Sample Code 1 1. Object Creation

This document contains 7 sections that provide Java code samples demonstrating object-oriented programming concepts in Java. These include: 1) Creating an object and accessing its properties, 2) Creating an object and getting input from the user, 3) Using a default constructor, 4) Creating an object in one class that is used in another class, 5) Using more than one constructor, 6) Creating an object that takes parameters, and 7) Examples of using if statements to make comparisons and conditional logic.

Uploaded by

Serkalem Negusse
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 views

Chapter Two Java Sample Code 1 1. Object Creation

This document contains 7 sections that provide Java code samples demonstrating object-oriented programming concepts in Java. These include: 1) Creating an object and accessing its properties, 2) Creating an object and getting input from the user, 3) Using a default constructor, 4) Creating an object in one class that is used in another class, 5) Using more than one constructor, 6) Creating an object that takes parameters, and 7) Examples of using if statements to make comparisons and conditional logic.

Uploaded by

Serkalem Negusse
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/ 4

Chapter two

Java Sample code 1


1. object creation
public class Object_creation {
int age;
String name;
public static void main(String[] args) {
Object_creation obj=new Object_creation();
obj.age=24;
obj.name="Sibhat";
System.out.println("Name is :"+obj.name);
System.out.println("Age of the Person is :"+obj.age);
}
}

2. object and input from user


import java.util.*;
public class input_from_user {
int age;
String name;
public static void main(String[] args) {
Scanner ss=new Scanner(System.in);
input_from_user obj=new input_from_user();
System.out.println("Enter the person name:");
obj.name=ss.next();
System.out.println("Enter the person Age:");
obj.age=Integer.parseInt(ss.next());

System.out.println("The Name of person is:"+obj.name);


System.out.println("The Age of person is:"+obj.age);}}
3. Object and default constructor
public class object_constractor {
int age;
String name;
object_constractor()// defualt constractor
{
this.age=24;
this.name="Sibhat";
}
public static void main(String[] args) {
object_constractor obj=new object_constractor();
System.out.println("The Name of person is:"+obj.name);
System.out.println("The Age of person is:"+obj.age);}}

4. Object creation with another class


public class rectangele {
int width,height;
public rectangele(){
this.width=9;
this.height=5;
}
public int area(){
return width*height;
}
public static void main(String[] args) {
rectangele obj=new rectangele();
System.out.println("The Area of the rectangle is:"+obj.area());}}
5. Object and more than one constructor
public class rectangele {
int width,height;
public rectangele(){
this.width=9;
this.height=5;
}
public int area(){
return width*height;
}
public static void main(String[] args) {
rectangele obj=new rectangele();
System.out.println("The area of the rectangle is:"+obj.area());
}
}
6. Object with parameter
public class rectangle_2 {
int width,height;
public rectangle_2(int w,int h)
{
this.width=w;
this.height=h;
}
public int area(){
return width*height;
}
public static void main(String[] args) {
rectangle_2 obj=new rectangle_2(10,7);
System.out.println("The area of the rectangle is:"+obj.area());
}
}
Exercise
Create a java program which accept n number of student id, student name and mark and calculate
the grade of the student based on the given mark

Student states
Id of student

Name of student

Mark of student

Student behavior
A method which calculate the grade
A method to display the name and grade of the student
Constructor member
Parameterize constructor
To initialize (accept) students information’s student id, name and mark
Note:-use this keyword to access member of class with in the class

7. Control statements
A) If statement
Question 1: write a program that take two number from the user and display the first
number is greater than, less than, or equal to, the second number
Question 2: write a program that take a number from the user and check the number
greater than zero, less than zero, or equal to zero
Question 3: write a program that take a number from the user and find the number is
even or odd
Question 4: write a program that take the final mark from the user and convert a final
grade in a scale of 0 to 100 to a letter grade (i.e. 90 to 100corrosponds A,80 to 89 is
B,70 to 79 is C ,60 to 69 is D and less than 60 is F)
Question 5:write a program that takes a number from the user and check a number is
less than 10,if so display a message below 10.if not check a number whether less than
60 or nor ,if so display a message below 60

You might also like