0% found this document useful (0 votes)
45 views4 pages

Sales Commrate Comrate: Computecommission

The document contains 4 programming exercises involving object-oriented concepts in Java: 1. Create a Commission class with methods to calculate commissions based on sales and rate, displaying the results. 2. Create a Pizza class to store topping, diameter, and price data with getter and setter methods. 3. Create a TestPizza class to instantiate a Pizza object and demonstrate using its methods. 4. Create a Circle class to represent a circle with radius, diameter, and area fields along with methods to set the radius, get radius, compute diameter from radius, and compute area from radius.

Uploaded by

Amira
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)
45 views4 pages

Sales Commrate Comrate: Computecommission

The document contains 4 programming exercises involving object-oriented concepts in Java: 1. Create a Commission class with methods to calculate commissions based on sales and rate, displaying the results. 2. Create a Pizza class to store topping, diameter, and price data with getter and setter methods. 3. Create a TestPizza class to instantiate a Pizza object and demonstrate using its methods. 4. Create a Circle class to represent a circle with radius, diameter, and area fields along with methods to set the radius, get radius, compute diameter from radius, and compute area from radius.

Uploaded by

Amira
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

User-defined & Constructors

Programming Exercises

1. Create a class named Commission that includes 3 variables: a double sales figure, a
double commission rate and an integer commission rate. Create 2 overload methods
named computeCommission(). The first method takes 2 double arguments representing
sales and rate. Multiply them and then display the result. The second method takes 2
arguments: a double sales figure and an integer commission rate. This method must
divide the commission rate by 100.00 before multiplying it by the sales figure. Display
the commission.

import javax.swing.*;
public class Commission{

double sales, commRate;


int comRate;

public static double computeCommission(double sales, double rate)


{

double results = sales * rate;


return results;
}

public static double computeCommission(double sales, int commRate)


{

double num = commRate/100.0;


double result = num * sales;
return result;
}

public static void main(String[] args)


{
JOptionPane.showMessageDialog(null, "First Commission method\n" +
computeCommission(550.50, 0.75) + "\nSecond Commission
method\n" +
computeCommission(135.00, 50));
}

2. Create a class named Pizza. Data fields include String for toppings, an integer for
diameter in inches and double for price. Include methods to get and set values for each
of these fields.

public class Pizza //class


{
private String toppings[];
private Integer diameter;
private Double Price;
int index;
public Pizza() // constructor
{ index=0;
toppings=new String [100];
}
public void setToppings(String toppings)/**to set all toppings of pizaa*/
{
this.toppings[index]=toppings;
index=index+1;
}
public void setDiameter(Integer diameter)/** to set Diameter(how big is
the pizza)*/
{
this.diameter=diameter;
}
public void setPricer(Double price)/**to set the price of pizaa*/
{
this.Price=price;
}
public String getToppings()/**to get all toppings of pizaa*/
{
String all="";
for (int x=0;x<index;x++){
all=all+"\n*"+this.toppings[x];
}
return all;
};
public Double getPricer()/**to get a price of pizaa*/
{
return Price;
}
public Integer getDiameter()/**to get all diameter of pizaa*/
{
return diameter;
}
}

3. Create a class named TestPizza that instantiates one Pizza object and demonstrates the
use of the Pizza set and get methods.

import javax.swing.JOptionPane;
public class TestPizza // class
{
public static void main (String []args)
{

Pizza romeo= new Pizza(); // constructor

String choice = "";


char yeah='y';
String toppings;
Integer Diameter;
Double price;
while(yeah=='y') //iterate because there are many toppings when it
comes to pizza
{
toppings=JOptionPane.showInputDialog("ENTER THE TOPPINGS:");
romeo.setToppings(toppings); // to set the toppings
choice=JOptionPane.showInputDialog("Are there any toppings Y/N:");
choice=choice.toLowerCase();
yeah=choice.charAt(0);
}
Diameter=Integer.parseInt(JOptionPane.showInputDialog("ENTER THE
Diameter(size):"));
romeo.setDiameter(Diameter); //to set Diameter (how big/small) the
pizza
price=Double.parseDouble(JOptionPane.showInputDialog("ENTER THE
PRICE:"));
romeo.setPricer(price); // to set price

JOptionPane.showMessageDialog(null,"INFORMATION\n\nTOPPINGS:"+romeo.getTopping
s()+"\nDiameter(inches):"+
romeo.getDiameter()+"\nPrice:"+romeo.getPricer());// to get
the toppings,diameter,price
}}

4. Create a class named Circle with fields named radius, area and diameter. Include
methods named setRadius(), getRadius(), computeDiameter() which computes a circle’s
diameter and computeArea() which computes a circle’s area. The diameter of a circle is
twice its radius and the area is A=π r 2 .

public class Circle

//declare the required variables

private double radius, diameter, area;

//definition of "setRadius" method

public void setRadius(double radius)


{

//set the value

this.radius = Math.abs(radius);

//definition of "getRadius" method

public double getRadius()


{

//return the value

return this.radius;

//definition of "getDiameter" method


public double getDiameter()
{

//return the diameter

return this.diameter;

//definition of "getArea" method

public double getArea()


{

//return the area

return this.area;

//definition of "computeDiameter" method

private void computeDiameter()


{

//calculate the diameter

diameter = radius*2;

//definition of "computeArea" method

private void computeArea()


{
//calculate the area

area = 3.14*radius*radius;
}

You might also like