Problem Solving #3

1 / 11

# Problem Solving #3 - PowerPoint PPT Presentation

Problem Solving #3. ICS 201 Introduction to Computer Science. Example 1. Declare a class named Employee that contains: A String variable called Name ; A double variable named Basic_Salary ; A Char variable called Gender (‘ M ’ for Male and ‘ F ’ for Female)

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about ' Problem Solving #3' - marika

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Problem Solving #3

ICS 201

Introduction to Computer Science

Example 1
• Declare a class named Employee that contains:
• A String variable called Name ;
• A double variable named Basic_Salary;
• A Char variable called Gender (‘M’ for Male and ‘F’ for Female)
• A constructor that initializes the field of the Employee class;
• An accessor method getGender( ) that returns the gender of an employee.

class Employee {

String name ;

double Basic_Salary ;

char Gender ;

Employee (String n , double b , char G) {

name = n ;

Basic_Salary = b ;

Gender = G ;

}

char getGender(){

return Gender ;

}

}

• An Integer variable named Extra_Hours;
• A constructor to initialize the different fields of Employee and Faculty classes;
• A method MontlySalary( ) that returns the monthly salary of a faculty;
• Hint: Monthly salary = Basic_Salary + Extra_hours*100

class Faculty extends Employee {

intExtra_Hours ;

Faculty (String n , double b , char G , int EA){

super(n , b , G) ;

Extra_Hours = EA ;

}

double MonthlySalary (){

return super.Basic_Salary + Extra_Hours*100 ;

}

}

• An Integer variable named Rank;
• A constructor to initialize the different fields of Employee and Staff classes;
• MontlySalary( ) that returns the monthly salary of a staff according to the Gender;

Hint :

Monthly salary = Basic_Salary + 500 if Gender = ‘M’;

Monthly salary = Basic_Salary + 300 if Gender = ‘F’;

class Staff extends Employee {

int Rank ;

Staff (String n , double b , char G , int R){

super(n , b , G) ;

Rank = R ;

}

double MonthlySalary (){

double MS=0 ;

if (super.getGender() == \'M\')

MS= super.Basic_Salary + 500 ;

if (super.getGender() == \'F\')

MS= super.Basic_Salary + 300 ;

return MS ;

}

}

Create an instance of Faculty class, initialize all the fields with appropriate values, and invoke the MontlySalary( ) method.

• Create an instance of Staff class, initialize all the fields with appropriate values, and invoke the MontlySalary( ) method.

class Test{

public static void main(String [] a){

Faculty F = new Faculty("xxxx" , 1220.5,\'F\',10) ;

System.out.println("The monthly salary of the faculty xxxx is " +F.MonthlySalary()) ;

Staff S = new Staff("yyyy" , 1350.5,\'M\',1) ;

System.out.println("The monthly salary of the staff yyyy is " +S.MonthlySalary()) ;

}

}

Example 2
• Consider the class named Book that contains:
• A private String variable called title;
• A private integer variable called nbpages;
• A public double variable called price ;
• Two public methods SetTitle() and SetNbpages();
• Two public methods getTitle() and getNbpages();
• Class TextBook is a derived class from the class Book. It contains:
• A protected integer variable called gradeLevel;
• A public method toString() that returns a description of a textBook;
• A public method PriceAfterDiscount() that returns the price of a book after applying a discount of 10%.
• Question: Establish the UML diagram for this problem.