1 / 11

Problem Solving #3

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)

marika
Download Presentation

Problem Solving #3

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem Solving #3 ICS 201 Introduction to Computer Science

  2. 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.

  3. 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 ; } }

  4. Declare a derived class from Employee called Faculty that contains: • 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

  5. 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 ; } }

  6. Declare a derived class from Employee called Staff that contains: • 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’;

  7. 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 ; } }

  8. 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.

  9. 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()) ; } }

  10. 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.

More Related