Complex Numbers with input from keyboard

(Complex Numbers) Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form
realPart + imaginaryPart * i
where i is
Write a program to test your class. Use int type variables to represent the private data of the class. All values should taken input.
a) Add two Complex numbers: The real parts are added together and the imaginary parts are added together.
b) Subtract two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.
c) Print Complex numbers in the form (realPart, imageinaryPart).

solution:

import java.util.Scanner;
class part{
int real;
int complex;
int real1;
int complex1;
part(){
Scanner input =new Scanner(System.in);
System.out.println("Enter first real value: ");
real=input.nextInt();
System.out.println("Enter first imaginary value: ");
complex=input.nextInt();
System.out.println("Your first value is:"+"a="+"("+real+"x+i"+complex+"y"+")");

System.out.println("Enter your second real value :");

real1=input.nextInt();
System.out.println("Enter second real value: ");
complex1=input.nextInt();
System.out.println("Your second value is:"+"a="+"("+real1+"x+i"+complex1+"y"+")");

}
void add(){
System.out.println("Adding two values: ");
int real2=real+real1;
int complex2=complex+complex1;
System.out.println("After adding:"+real2+"x"+"+i"+complex2+"y");

}
void substruct(){
System.out.println("substracting two values : ");

int real3=real-real1;
int complex3=complex-complex1;
if(complex3>=0)
System.out.println("After substracting values: "+real3+"x"+"-"+complex3+"i");
else
System.out.println("After substracting values: "+real3+"x"+""+complex3+"i");

}

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

part oikoi=new part();

oikoi.add();
oikoi.substruct();

}
}

Advertisements

Complex Numbers

(Complex Numbers) Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form
realPart + imaginaryPart * i
where i is
Write a program to test your class. Use floating-point variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform the following operations:
a) Add two Complex numbers: The real parts are added together and the imaginary parts are added together.
b) Subtract two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.
c) Print Complex numbers in the form (realPart, imageinaryPart).

Solution

public class no13
{
private double real;
private double imaginary;

// Initialize both parts to 0
public no13()
{
this( 0.0, 0.0 );
} // end Complex no-argument constructor

public no13( double r, double i )
{
real = r;
imaginary = i;
} // end Complex two-argument constructor
// Add two Complex numbers
public no13 add( no13 right )
{
return new no13( real + right.real,
imaginary + right.imaginary );
} // end method add
// Subtract two Complex numbers
public no13 subtract( no13 right )
{
return new no13( real - right.real,
imaginary - right.imaginary );
} // end method subtract

// Return String representation of a Complex number
public String toString()
{
return String.format( "(%.1f, %.1f)", real, imaginary );
} // end method toComplexString;
} // end class Complex

class ComplexTest
{
public static void main( String args[] )
{
// initialize two numbers
no13 a = new no13( 9.5, 7.7 );
no13 b = new no13( 1.2, 3.1 );

System.out.printf( "a = %s\n", a.toString() );
System.out.printf( "b = %s\n", b.toString() );
System.out.printf( "a + b = %s\n", a.add( b ).toString() );
System.out.printf( "a - b = %s\n", a.subtract( b ).toString() );
} // end main
} // end class ComplexTest

solved by : arifur rahman

Printing the Decimal Equivalent of a Binary Number

(Printing the Decimal Equivalent of a Binary Number) Write a program that inputs an integer containing only 0s and 1s (i.e., a binary integer) and prints its decimal equivalent. [Hint: Use the remainder and division operators to pick off the binary number’s digits one at a time, from right to left. In the decimal number system, the rightmost digit has a positional value of 1 and the next digit to the left a positional value of 10, then 100, then 1000, and so on. The decimal number 234 can be interpreted as 4 * 1 + 3 * 10 + 2 * 100. In the binary number system, the rightmost digit has a positional value of 1, the next digit to the left a positional value of 2, then 4, then 8, and so on. The decimal equivalent of binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or, 13.]

Solution:

import java.util.Scanner;

public class binary {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int bNumber,bNumberCopy,i=7,dNumber=0; int digit[] = new int[8];
//String strBinaryNumber = "11011001";

System.out.print("\nEnter binary number(max digits: 8) :");
bNumber = sc.nextInt();
if((Integer.toString(bNumber)).length() 0){
digit[i] = bNumberCopy % 10;
bNumberCopy /= 10;
i--;
}
for(int j=7;j>=0;j--){
dNumber += (Math.pow(2,(7-j))*digit[j]);
}
System.out.print("\nDecimal form of '" + bNumber + "' is :" + dNumber);
}else{
System.out.print("\nInvalid input,,,,program terminates.");
}
}
}

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