Rectangle Class-11

(Rectangle Class) Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide methods that calculate the rectangle’s perimeter and area. It has set and get methods for both length and width. The set methods should verify that length and width are each floating-point numbers larger than 0.0 and less than 20.0.Write a program to test class Rectangle.

import java.util.Scanner;

class rectangle{
public float width=1;
private float length=1;
void get(){
Scanner indata=new Scanner(System.in);

System.out.println(“Enter width:”);

width=indata.nextFloat();
System.out.println(“Enter length: “);
length=indata.nextFloat();

}

void set(){
if (width>0&&width0&&length0&&width0&&length0&&width0&&length<20){
System.out.println("Area is : "+(width+length));
}

}

}

}
public class noofthe12 {
public static void main(String arg[]){

rectangle rec=new rectangle ();
rec.get();
rec.set();

}

}

Advertisements

class-2

Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a program named InvoiceTest that demonstrates class Invoice’s capabilities.

Solution:
//invoice class

import java.util.Scanner;
class invoice{
public String name;
public String description;
public int item_number;
public double price;
//invoice constractor
invoice(){
name="";
description="";
item_number=0;
price=0.00;

}
//set method
void set(){
Scanner input=new Scanner(System.in);

System.out.println("Name of the Company: ");
name=input.nextLine();
System.out.println("Types of the company: ");
description=input.nextLine();
System.out.println("Amount of item:");
item_number=input.nextInt();
System.out.println("Price of item: ");
price=input.nextDouble();

}

void get(){
System.out.println("Your company name is:"+name);
System.out.println("Your company is type of :"+description);
System.out.println("Your item amount: "+item_number);
System.out.println("Price of each item: "+price);

}
void getInvoiceAmmount(){
double total=item_number*price;
if(price>0)
System.out.println("Total price is :"+total);
if(price<=0)
System.out.println("Price is "+0.00);
}

}

public class no2 {
public static void main(String arg[]){
invoice oikoi=new invoice();
oikoi.set();
oikoi.get();
oikoi.getInvoiceAmmount();

}

}

class-5


Credit Limit Calculator) Write a program that determines whether any of several department-store customers has exceeded the credit limit on a charge account. For each customer, the following facts are available:
a) account number
b) balance at the beginning of the month
c) total of all items charged by the customer this month
d) total of all credits applied to the customer’s account this month
e) allowed credit limit.
The program should input all these facts as integers, calculate the new balance (= beginning balance + charges – credits), display the new balance and determine whether the new balance exceeds the customer’s credit limit. For those customers whose credit limit is exceeded, the program should display the message “Credit limit exceeded”.

solution:


import java.util.Scanner;
class custommeraccount{
public int ac_no;
public int bigining_balanc;
public int total_charge;
public int credit_apply;
public int new_balance;
custommeraccount(){
Scanner indata=new Scanner(System.in);
System.out.println("Enter your acount No: ");
ac_no=indata.nextInt();
System.out.println("Enter begining balance :");
bigining_balanc=indata.nextInt();
System.out.println("Enter total charge:");
total_charge=indata.nextInt();
System.out.println("Enter how much credit applies: ");
credit_apply=indata.nextInt();

}
void show(){
System.out.println("Accoutn no is: "+ac_no);
System.out.println("Beggining balance is : "+bigining_balanc);
System.out.println("Total charge is : " +total_charge);
System.out.println("Total credit applies: "+credit_apply);

}
int newbalance(){

new_balance=(bigining_balanc+total_charge-credit_apply);
return new_balance;

}
void show_newbalance(){
System.out.println("your new balance is: "+new_balance);
if (new_balance==0)
System.out.println("your Balance is Zero");
if(new_balance<0)
System.out.println("Yout credit balance limit exceedited");

}

}
public class no5 {
public static void main(String arg[]){
custommeraccount oikoi=new custommeraccount();
oikoi.show();
oikoi.newbalance();
oikoi.show_newbalance();

}

}

class-4

Create a class called Date that includes three instance variables—a month (type int),
a day (type int) and a year (type int).
Provide a constructor that initializes the three instance variables and assumes that
the values provided are correct.
Provide a set and a get method for each instance variable. Provide a method displayDate
that displays the month, day and
year separated by forward slashes (/). Write a program named DateTest that demonstrates
class Date’s capabilities.

Solution:


import java.util.Scanner;
class date{
private int day;
private int month;
private int year;
date(){
day=0;
month=0;
year=0;

}
void set_date(){
Scanner input =new Scanner(System.in);
System.out.println("Enter day , month ,year");

day=input.nextInt();
month=input.nextInt();
year=input.nextInt();

}
int day_get(){
return day;

}
int month_get(){
return month;

}
int year_get(){
return year;
}

void dispalay_date(){
System.out.println("date is: "+day+"/"+month+"/"+year);

}

}
public class no4 {
public static void main(String arg[]){
date oikoi=new date();
oikoi.set_date();
oikoi.day_get();
oikoi.month_get();
oikoi.year_get();
oikoi.dispalay_date();

}
}

class-3

Create a class called Employee that includes three instance variables—a first name
(type String), a last name (type String) and a monthly salary (double). Provide a
constructor that initializes the three instance variables. Provide a set and a get
method for each instance variable. If the monthly salary is not positive, set it
to 0.0. Write a program named EmployeeTest that demonstrates class Employee’s
capabilities. Create two Employee objects and display each object’s yearly salary.
Then give each Employee a 10% raise and display each Employee’s yearly salary again.

solution:

import java.util.Scanner;

class employee{
private String firstname;
private String lastname;
double monthly_salary;
employee(){

firstname="";
lastname="";
monthly_salary=0.00;

}

public void set(){

Scanner input=new Scanner(System.in);
System.out.println("Enter your first name:");

firstname=input.nextLine();
System.out.println("Enter your Last name: ");
lastname=input.nextLine();
System.out.println("Enter your monthly salary: ");
monthly_salary=input.nextDouble();

}
public void get(){
System.out.println("Your first name is: "+firstname);
System.out.println("Your last name is : "+lastname);
System.out.println("");
if(monthly_salary>0){
System.out.println("Your monthly salary is:"+ monthly_salary );
}
else System.out.println("Your monthly salary is: 0.00");
}

}
public class no3 {
public static void main(String arg[]){

employee emp=new employee();

emp.set();
emp.get();

double ys=emp.monthly_salary*12;
System.out.println("yearly salary is:"+ys);
double yes=(emp.monthly_salary*12+((ys*10)/100));// (ys+(ys*10)/100) korle o hobe.
System.out.println("Encreasing yearly salary is: "+yes);

}
}

Class 02

Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables—a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a program named InvoiceTest that demonstrates class Invoice’s capabilities.


Solution:

//invoice class
import java.util.Scanner;
class invoice{
public String name;
public String description;
public int item_number;
public double price;
//invoice constractor
invoice(){
name="";
description="";
item_number=0;
price=0.00;

}
//set method
void set(){
Scanner input=new Scanner(System.in);

System.out.println("Name of the Company: ");
name=input.nextLine();
System.out.println("Types of the company: ");
description=input.nextLine();
System.out.println("Amount of item:");
item_number=input.nextInt();
System.out.println("Price of item: ");
price=input.nextDouble();

}

void get(){
System.out.println("Your company name is:"+name);
System.out.println("Your company is type of :"+description);
System.out.println("Your item amount: "+item_number);
System.out.println("Price of each item: "+price);

}
void getInvoiceAmmount(){
double total=item_number*price;
if(price>0)
System.out.println("Total price is :"+total);
if(price<=0)
System.out.println("Price is "+0.00);
}
}

public class no2 {
public static void main(String arg[]){
invoice oikoi=new invoice();
oikoi.set();
oikoi.get();
oikoi.getInvoiceAmmount();

}

}

class-1

Write a program that inputs one number consisting of five digits from the user,
separates the number into its individual digits and prints the digits separated
from one another by three spaces each. For example, if the user types in the number
42339, the program should print
4 2 3 3 9


Solution:

import java.util.*;

public class Qn1 {

public static void main(String arg[])
{
String number;
int len,num,t,pnum,i,j;

Scanner input= new Scanner(System.in);
while(true)
{
System.out.println("Enter 5 digit number::\tTo stop press Q/q");
number=input.nextLine();

len=number.length();
if(number.equalsIgnoreCase("Q"))
break;
if(len5)
continue;

num=Integer.parseInt(number);

for(i=1;i<len;i++)
{
t=1;
for(j=i;j<len;j++)
{

t=t*10;

}

pnum=num/t;
num=num%t;

if(pnum==0)
{
System.out.printf("%3d", num);
}
else
{
System.out.printf("%3d", pnum);
}

}
System.out.println();

}

}

}

solved by faysal hasan.