Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Programming Asignment

Uploaded by

tilayefkadie
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Programming Asignment

Uploaded by

tilayefkadie
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

GAMBELLA UNIVERSTY

FACULTY OF NATURAL AND COMPUTAIONAL SCIENCE


DEPARTMENT OF COMPUTER SCIENCE

COURCE TITLE:OOP
Summitted by:

Name Id
Fitsum 5302
teshome
Birhan fentie 7774
Temesgen 6588
dereje
Fikade tilaye 5607
Tizazu 6402
sitotawu

Summitted to:Eyoas

Summistion date:10/4/2016

Ethiopian -Gambella
1. Write a Java program to create a class called "Person" with a name and age
attribute. Create two instances of the "Person" class, set their attributes using the
constructor, and print their name and age.
1.// Person.java
2.Public class Person {
3. Private String name;
4.Private int age;
5.Public Person (String name, int age) {
6.this.name = name;
7.this.age = age;
8.}
9.Public String getName() {
10.Return name;
11.}
12.Public int getAge() {
13.return age;
14.}
15. }

2. Write a Java program to create a class called "Dog" with a name and breed
attribute. Create two instances of the "Dog" class, set their attributes using the
constructor and modify the attributes using the setter methods and print the
updated values.
1.// Dog.java
2.Public class Dog {

3.Private String name;

4. Private String breed;

5.Public Dog(String name, String breed) {

6.this.name = name;

7.this.breed = breed;

8.}

9. Public String getName() {

10. return name;

11. }

12. public void setName(String name) {

13. this.name = name;

14. }

15. public String getBreed() {

16. return breed;

17. }

18. public void setBreed(String breed) {

19. this.breed = breed;

20. }

21.}
3. Write a Java program to create a class called "Rectangle" with width and
height attributes. Calculate the area and perimeter of the rectangle.

1.//Rectangle.java
2.public class Rectangle {
3. private double width;
4. private double height;

5. public Rectangle(double width, double height) {


6. this.width = width;
7. this.height = height;
8. }
9. public double getWidth() {
10. return width;
11. }
12. public void setWidth(double width) {
13. this.width = width;
14. }
15. Public double getHeight() {
16. return height;
17. }
18. public void setHeight(double height) {
19. this.height = height;
20. }
21. public double getArea() {
22. return width * height;
23. }
24. public double getPerimeter() {
25. return 2 * (width + height);
26. }
27.}
4. Write a Java program to create a class called "Circle" with a radius attribute.
You can access and modify this attribute. Calculate the area and circumference
of the circle.

1. //Circle.java
2. public class Circle {
3.private double radius;

4. public Circle(double radius) {


5. this.radius = radius;
6. }
7. Public double getradius() {
8. return radius;
9. }
10. public void setRadius(double radius) {
11. this.radius = radius;
12. }

13. public double getArea() {


14. return Math.PI * radius * radius;
15. }

16. public double getCircumference() {


17. return 2 * Math.PI * radius;
18. }
19.}

5. Write a Java program to create a class called "Employee" with a name, job
title, and salary attributes, and methods to calculate and update salary.

1.//Employee.java
2.Public class Employee {
3. Private String name;
4. Private String jobTitle;
5.Private double salary;

6. public Employee(String name, String jobTitle, double salary)


{
7. this.name = name;
8. this.jobTitle = jobTitle;
9. this.salary = salary;
10. }

11. public String getName() {


12. return name;
13. }

14. public void setName(String name) {


15. this.name = name;
16. }

17. public String getJobTitle() {


18. return jobTitle;
19. }

20. public void setJobTitle(String jobTitle) {


21. this.jobTitle = jobTitle;
22. }

23. public double getSalary() {


24. return salary;
25. }

26.public void setSalary(double salary) {


27. this.salary = salary;
28. }

29. public void raiseSalary(double percentage) {


30. salary = salary + salary * percentage / 100;
31. }

32. public void printEmployeeDetails() {


33. System.out.println("Name: " + name);
34. System.out.println("Job Title: " + jobTitle);
35. System.out.println("Salary: " + salary);
36. }
37.}

6. Write a Java program to create a class called "Shape" with abstract methods
for calculating area and perimeter, and subclasses for "Rectangle", "Circle", and
"Triangle".

1.//Rectangle.java
2.public class Rectangle extends Shape {
3.private double length;
4. private double width;

5. public Rectangle(double length, double width) {


6. this.length = length;
7. this.width = width;
8. }
9.public double getArea() {
10. return length * width;
11. }
12. Public double getPerimeter() {
13. return 2 * (length + width);
14. }
15.}

You might also like