المشروع طالب عمل آلة حاسبة تقوم بالعمليات الاساسية لعدة انظمة من الارقام
الجمع
الطرح
الضرب
القسمة
للانظمة التالية
الثنائي
العشري
الثماني
والتحويل من نظام الي آخر
في هذا الشرح يوجد كسور والتي سنتجاهلها في عمل المشروع لانه حسب ما هو موضح في المشروع المطلوب الاعداد الصحيحة فقط
طبعا طلب منا نعمل كلاسات
الكلاس الاساسية Number
وثلاث كلاسات اخرى ترث من الكلاس الاساسية
Binary Decimal Octal
قبل ما نبدا في عمل الكلاسات خلينا نفهم الانظمة كيف بتم التعامل معها
النظام الثنائي Binary
إن الأساس المستعمل في النظام الثنائي هو 2 ويتكون هذا النظام من رقمين فقط هما 0 و1 ويسمى كل منهما رقماً ثنائياً Binary Digit
ولتمثيل كل من الرقمين 0 و 1 فأنه لا يلزم إلا خانة واحدة, ولهذا السبب أصبح من الشائع أطلاق اسم بت Bit على الخانة التي يحتلها الرقم داخل العدد الثنائي
التحويل من النظام الثنائي إلى النظام العشري :
لتحويل أي عدد ثنائي إلى مكافئه العشري فإنه يجب علينا استعمال قانون التمثيل الموضعي للأعداد
و ينطبق هذا القانون عندما يكون الرقم الثنائي صحيحاً أو كسراً مع مراعاة أن أساس نظام العد هنا هو 2

مثال حول العدد الثنائي التالي إلى مكافئه العشري:

تحويل الأعداد من النظام العشري إلى الثنائي :
لتحويل أي عدد صحيح موجب من النظام العشري إلى الثنائي نستعمل طريقة الباقي Remainder Method الموضحة كالآتي:
1. أقسم العدد العشري على الأساس 2 .
2. أحسب باقي القسمة الذي يكون أما 1 أو 0 .
3. أقسم ناتج القسمة السابق على الأساس 2 كما في خطوة (1).
4. أحسب باقي القسمة كما في خطوة (2).
5. استمر في عملية القسمة وتحديد الباقي حتى يصبح خارج القسمة الصحيح صفراً.
6. العدد الثنائي المطلوب يتكون من أرقام الباقي مقروءة من الباقي الأخير إلى الأول (لاحظ أن الباقي الأول يمثل LSD بينما يمثل الباقي الأخير MSD ).
مثال لتحويل الرقم 12 من النظام العشري إلى الثنائي نتبع الآتي:
ناتج القسمة الباقي
12 ÷2 =6 0 الخانة الأدنى منزلة LSD
0 6÷2 =3
1 3÷2 =1
1 1÷2 =0 الخانة الأعلى منزلة MSD
فيكون الناتج (من أسفل إلى أعلى ومن اليسار إلى اليمين):

النظام الثماني Octal System :
كما هو معروف فإن أساس النظام الثماني هو العدد 8.وتتكون رموز هذا النظام من الأرقام
التحويل من النظام الثماني إلى العشري:
للتحويل من النظام الثماني إلى النظام العشري يستعمل قانون التمثيل الموضعي للأعداد مع مراعاة أن أساس نظام العد هنا هو 8 .
مثال حول العدد الثماني إلى مكافئه العشري ؟

الناتج :

تحويل من النظام العشري إلى الثماني:
مثال حول العدد العشري 122 إلى مكافئه الثماني؟
ناتج القسمة الباقي
122÷8=15 2
15÷8=1 7
1÷8=0 1
إنهاء القسمة
فيكون الناتج (من أسفل إلى أعلى ومن اليسار إلى اليمين):

وهذا ما سنتبعه في عمل المشروع
نبدا في البرمجة و نعمل الكلاسات اللي موضحة بالمشروع
الكلاس الاول Number
كود
public class Number{
public String number;
void printInFormat(){
System.out.println("The result is ***"+number);
}
}
public String number;
void printInFormat(){
System.out.println("The result is ***"+number);
}
}
كلاس النظام الثنائي Binary
كود
public class Binary extends Number{
boolean check_B(){
boolean check=false;
for (int i = 0; i {
if(Character.getNumericValue(number.charAt(i))>1||Character.getNumericValue(number.charAt(i))<0)
{
System.out.println("Be attention!!!!! Your number must be in binary format.");
check= false;
break;
}
else
check= true;
}
return check;
}
int convertToDecimal(){
int n=0;
for (int i = number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==number.length()-1)
{
n+=Character.getNumericValue(number.charAt(j))*Math.pow(2,i);
}
}
}
return n;
}
int SumTwoBinary(Binary b){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = b.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==b.number.length()-1)
{
x+=Character.getNumericValue(b.number.charAt(j))*Math.pow(2,i);
}
}
}
y=n+x;
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int SubTwoBinary(Binary b){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = b.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==b.number.length()-1)
{
x+=Character.getNumericValue(b.number.charAt(j))*Math.pow(2,i);
}
}
}
y=n-x;
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int MultiTwoBinary(Binary b){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = b.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==b.number.length()-1)
{
x+=Character.getNumericValue(b.number.charAt(j))*Math.pow(2,i);
}
}
}
y=n*x;
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int DivTwoBinary(Binary b){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = b.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==b.number.length()-1)
{
x+=Character.getNumericValue(b.number.charAt(j))*Math.pow(2,i);
}
}
}
y=n/x;
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
}
boolean check_B(){
boolean check=false;
for (int i = 0; i
if(Character.getNumericValue(number.charAt(i))>1||Character.getNumericValue(number.charAt(i))<0)
{
System.out.println("Be attention!!!!! Your number must be in binary format.");
check= false;
break;
}
else
check= true;
}
return check;
}
int convertToDecimal(){
int n=0;
for (int i = number.length(); i>=0; --i)
{
for (int j = 0; j
if((i+j)==number.length()-1)
{
n+=Character.getNumericValue(number.charAt(j))*Math.pow(2,i);
}
}
}
return n;
}
int SumTwoBinary(Binary b){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = b.number.length(); i>=0; --i)
{
for (int j = 0; j
if((i+j)==b.number.length()-1)
{
x+=Character.getNumericValue(b.number.charAt(j))*Math.pow(2,i);
}
}
}
y=n+x;
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int SubTwoBinary(Binary b){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = b.number.length(); i>=0; --i)
{
for (int j = 0; j
if((i+j)==b.number.length()-1)
{
x+=Character.getNumericValue(b.number.charAt(j))*Math.pow(2,i);
}
}
}
y=n-x;
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int MultiTwoBinary(Binary b){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = b.number.length(); i>=0; --i)
{
for (int j = 0; j
if((i+j)==b.number.length()-1)
{
x+=Character.getNumericValue(b.number.charAt(j))*Math.pow(2,i);
}
}
}
y=n*x;
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int DivTwoBinary(Binary b){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = b.number.length(); i>=0; --i)
{
for (int j = 0; j
if((i+j)==b.number.length()-1)
{
x+=Character.getNumericValue(b.number.charAt(j))*Math.pow(2,i);
}
}
}
y=n/x;
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
}
كلاس النظام العشري Decimal
كود
public class Decimal extends Number{
boolean check_D(){
boolean check=false;
for (int i = 0; i {
if(Character.getNumericValue(number.charAt(i))>9||Character.getNumericValue(number.charAt(i))<0)
{
System.out.println("Be attention!!!!! Your number must be in decimal format.");
check= false;
break;
}
else
check= true;
}
return check;
}
int convertToBinary(){
int y=Integer.parseInt(this.number);
String result="";
String last="";
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int convertToOctal(){
int y=Integer.parseInt(this.number);
String result="";
String last="";
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int SumTwoDec(Decimal d){
int y=Integer.parseInt(this.number);
int z=Integer.parseInt(d.number);
return y+z;
}
int SubTwoDec(Decimal d){
int y=Integer.parseInt(this.number);
int z=Integer.parseInt(d.number);
return y-z;
}
int MultiTwoDec(Decimal d){
int y=Integer.parseInt(this.number);
int z=Integer.parseInt(d.number);
return y*z;
}
int DivTwoDec(Decimal d){
int y=Integer.parseInt(this.number);
int z=Integer.parseInt(d.number);
return y/z;
}
}
boolean check_D(){
boolean check=false;
for (int i = 0; i
if(Character.getNumericValue(number.charAt(i))>9||Character.getNumericValue(number.charAt(i))<0)
{
System.out.println("Be attention!!!!! Your number must be in decimal format.");
check= false;
break;
}
else
check= true;
}
return check;
}
int convertToBinary(){
int y=Integer.parseInt(this.number);
String result="";
String last="";
boolean end=false;
while(!end)
{
result+=y%2;
y=y/2;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int convertToOctal(){
int y=Integer.parseInt(this.number);
String result="";
String last="";
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int SumTwoDec(Decimal d){
int y=Integer.parseInt(this.number);
int z=Integer.parseInt(d.number);
return y+z;
}
int SubTwoDec(Decimal d){
int y=Integer.parseInt(this.number);
int z=Integer.parseInt(d.number);
return y-z;
}
int MultiTwoDec(Decimal d){
int y=Integer.parseInt(this.number);
int z=Integer.parseInt(d.number);
return y*z;
}
int DivTwoDec(Decimal d){
int y=Integer.parseInt(this.number);
int z=Integer.parseInt(d.number);
return y/z;
}
}
كلاس النظام الثماني Octal
كود
public class Octal extends Number{
boolean check_Oc(){
boolean check=false;
for (int i = 0; i {
if(Character.getNumericValue(number.charAt(i))>7||Character.getNumericValue(number.charAt(i))<0)
{
System.out.println("Be attention!!!!! Your number must be in octal format.");
check= false;
break;
}
else
check= true;
}
return check;
}
int convertToDecimal(){
int n=0;
for (int i = number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==number.length()-1)
{
n+=Character.getNumericValue(number.charAt(j))*Math.pow(8,i);
}
}
}
return n;
}
int SumTwoOctal(Octal c){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = c.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==c.number.length()-1)
{
x+=Character.getNumericValue(c.number.charAt(j))*Math.pow(8,i);
}
}
}
y=n+x;
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int SubTwoOctal(Octal c){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = c.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==c.number.length()-1)
{
x+=Character.getNumericValue(c.number.charAt(j))*Math.pow(8,i);
}
}
}
y=n-x;
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int MultiTwoOctal(Octal c){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = c.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==c.number.length()-1)
{
x+=Character.getNumericValue(c.number.charAt(j))*Math.pow(8,i);
}
}
}
y=n*x;
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int DivTwoOctal(Octal c){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = c.number.length(); i>=0; --i)
{
for (int j = 0; j {
if((i+j)==c.number.length()-1)
{
x+=Character.getNumericValue(c.number.charAt(j))*Math.pow(8,i);
}
}
}
y=n/x;
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
}
boolean check_Oc(){
boolean check=false;
for (int i = 0; i
if(Character.getNumericValue(number.charAt(i))>7||Character.getNumericValue(number.charAt(i))<0)
{
System.out.println("Be attention!!!!! Your number must be in octal format.");
check= false;
break;
}
else
check= true;
}
return check;
}
int convertToDecimal(){
int n=0;
for (int i = number.length(); i>=0; --i)
{
for (int j = 0; j
if((i+j)==number.length()-1)
{
n+=Character.getNumericValue(number.charAt(j))*Math.pow(8,i);
}
}
}
return n;
}
int SumTwoOctal(Octal c){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = c.number.length(); i>=0; --i)
{
for (int j = 0; j
if((i+j)==c.number.length()-1)
{
x+=Character.getNumericValue(c.number.charAt(j))*Math.pow(8,i);
}
}
}
y=n+x;
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int SubTwoOctal(Octal c){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = c.number.length(); i>=0; --i)
{
for (int j = 0; j
if((i+j)==c.number.length()-1)
{
x+=Character.getNumericValue(c.number.charAt(j))*Math.pow(8,i);
}
}
}
y=n-x;
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int MultiTwoOctal(Octal c){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = c.number.length(); i>=0; --i)
{
for (int j = 0; j
if((i+j)==c.number.length()-1)
{
x+=Character.getNumericValue(c.number.charAt(j))*Math.pow(8,i);
}
}
}
y=n*x;
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
int DivTwoOctal(Octal c){
int n=this.convertToDecimal();
int x=0;int y=0;
String result="";String last="";
for (int i = c.number.length(); i>=0; --i)
{
for (int j = 0; j
if((i+j)==c.number.length()-1)
{
x+=Character.getNumericValue(c.number.charAt(j))*Math.pow(8,i);
}
}
}
y=n/x;
boolean end=false;
while(!end)
{
result+=y%8;
y=y/8;
if(y==0)
end=true;
}
for (int i = result.length()-1; i>=0; i--)
last+=result.charAt(i);
return Integer.parseInt(last);
}
}
الان الكلاس الرئيسي Test
كود
import java.util.Scanner;
public class Test{
static Scanner input=new Scanner(System.in);
public static void main(String arg[]){
int ch=0;
do{
System.out.println("**********************************************************************");
System.out.println("1.Decimal 2.Binary 3.Octal 4.Conversions 5.Quit");
System.out.println("**********************************************************************");
System.out.print("SHOOSE YOUR SYSTEM: ");
ch=input.nextInt();
switch(ch){
case 1:Decimal();break;
case 2:Binary();break;
case 3:Octal();break;
case 4:Conversions();break;
case 5:Quit();break;
}
}while(ch!=5);
}
public static void Decimal(){
char c='0';
System.out.println("\t\t A) Add two Decimal");
System.out.println("\t\t S) Subtract two Decimal");
System.out.println("\t\t M) Multiple two Decimal");
System.out.println("\t\t D) Divide two Decimal");
System.out.println("\t\t B) Back to main menu");
do{
System.out.println("Enter your formula:");
System.out.print("## ");
String in=input.next();
c=in.charAt(0);
switch(c){
case 'A':adddecimal();break;
case 'S':subdecimal();break;
case 'M':muldecimal();break;
case 'D':divdecimal();break;
case 'B':System.out.println("Finished section............");
}
}while(c!='B');
}
public static void adddecimal(){
String first=input.next();
String second=input.next();
Decimal d1=new Decimal();
Decimal d2=new Decimal();
d1.number=first;
d2.number=second;
if(d1.check_D()&&d2.check_D()){
int ans=d1.SumTwoDec(d2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void subdecimal(){
String first=input.next();
String second=input.next();
Decimal d1=new Decimal();
Decimal d2=new Decimal();
d1.number=first;
d2.number=second;
if(d1.check_D()&&d2.check_D()){
int ans=d1.SubTwoDec(d2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void muldecimal(){
String first=input.next();
String second=input.next();
Decimal d1=new Decimal();
Decimal d2=new Decimal();
d1.number=first;
d2.number=second;
if(d1.check_D()&&d2.check_D()){
int ans=d1.MultiTwoDec(d2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void divdecimal(){
String first=input.next();
String second=input.next();
Decimal d1=new Decimal();
Decimal d2=new Decimal();
d1.number=first;
d2.number=second;
if(d1.check_D()&&d2.check_D()){
int ans=d1.DivTwoDec(d2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void Binary(){
char c='0';
System.out.println("\t\t A) Add two binary");
System.out.println("\t\t S) Subtract two binary");
System.out.println("\t\t M) Multiple two binary");
System.out.println("\t\t D) Divide two binary");
System.out.println("\t\t B) Back to main menu");
do{
System.out.println("Enter your formula:");
System.out.print("## ");
String in=input.next();
c=in.charAt(0);
switch(c){
case 'A':addbinary();break;
case 'S':subbinary();break;
case 'M':mulbinary();break;
case 'D':divbinary();break;
case 'B':System.out.println("Finished section............");
}
}while(c!='B');
}
public static void addbinary(){
String first=input.next();
String second=input.next();
Binary b1=new Binary();
Binary b2=new Binary();
b1.number=first;
b2.number=second;
if(b1.check_B()&&b2.check_B()){
int ans=b1.SumTwoBinary(b2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void subbinary(){
String first=input.next();
String second=input.next();
Binary b1=new Binary();
Binary b2=new Binary();
b1.number=first;
b2.number=second;
if(b1.check_B()&&b2.check_B()){
int ans=b1.SubTwoBinary(b2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void mulbinary(){
String first=input.next();
String second=input.next();
Binary b1=new Binary();
Binary b2=new Binary();
b1.number=first;
b2.number=second;
if(b1.check_B()&&b2.check_B()){
int ans=b1.MultiTwoBinary(b2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void divbinary(){
String first=input.next();
String second=input.next();
Binary b1=new Binary();
Binary b2=new Binary();
b1.number=first;
b2.number=second;
if(b1.check_B()&&b2.check_B()){
int ans=b1.DivTwoBinary(b2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void Octal(){
char c='0';
System.out.println("\t\t A) Add two Octal");
System.out.println("\t\t S) Subtract two Octal");
System.out.println("\t\t M) Multiple two Octal");
System.out.println("\t\t D) Divide two Octal");
System.out.println("\t\t B) Back to main menu");
do{
System.out.println("Enter your formula:");
System.out.print("## ");
String in=input.next();
c=in.charAt(0);
switch(c){
case 'A':addOctal();break;
case 'S':subOctal();break;
case 'M':mulOctal();break;
case 'D':divOctal();break;
case 'B':System.out.println("Finished section............");
}
}while(c!='B');
}
public static void addOctal(){
String first=input.next();
String second=input.next();
Octal o1=new Octal();
Octal o2=new Octal();
o1.number=first;
o2.number=second;
if(o1.check_Oc()&&o2.check_Oc()){
int ans=o1.SumTwoOctal(o2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void divOctal(){
String first=input.next();
String second=input.next();
Octal o1=new Octal();
Octal o2=new Octal();
o1.number=first;
o2.number=second;
if(o1.check_Oc()&&o2.check_Oc()){
int ans=o1.DivTwoOctal(o2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void subOctal(){
String first=input.next();
String second=input.next();
Octal o1=new Octal();
Octal o2=new Octal();
o1.number=first;
o2.number=second;
if(o1.check_Oc()&&o2.check_Oc()){
int ans=o1.SubTwoOctal(o2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void mulOctal(){
String first=input.next();
String second=input.next();
Octal o1=new Octal();
Octal o2=new Octal();
o1.number=first;
o2.number=second;
if(o1.check_Oc()&&o2.check_Oc()){
int ans=o1.MultiTwoOctal(o2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void Conversions(){
int c=0;
System.out.println("\n This section is designed to convert from one system to another.....\n");
System.out.println("1- Decimal to Binary\t3- Decimal to Octal\t5- Binary to Octal");
System.out.println("2- Binary to Decimal\t4- Octal to Decimal\t6- Octal to Binary");
System.out.println("\t\t 7- Back to Main");
System.out.println("****************************************************************************");
do{
System.out.print("\nENTER YOUR CHOICE: ");
c=input.nextInt();
switch(c){
case 1:D2B();break;
case 2:B2D();break;
case 3:D2O();break;
case 4:O2D();break;
case 5:B2O();break;
case 6:O2B();break;
case 7:System.out.println("Finished section............");
}
}while(c!=7);
}
public static void D2B(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Decimal d1=new Decimal();
d1.number=first;
if(d1.check_D())
{
int ans=d1.convertToBinary();
System.out.println("The result is *** "+ans);
}else return;
}
public static void B2D(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Binary b1=new Binary();
b1.number=first;
if(b1.check_B())
{
int ans=b1.convertToDecimal();
System.out.println("The result is *** "+ans);
}else return;
}
public static void D2O(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Decimal d1=new Decimal();
d1.number=first;
if(d1.check_D())
{
int ans=d1.convertToOctal();
System.out.println("The result is *** "+ans);
}else return;
}
public static void O2D(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Octal o1=new Octal();
o1.number=first;
if(o1.check_Oc())
{
int ans=o1.convertToDecimal();
System.out.println("The result is *** "+ans);
}else return;
}
public static void B2O(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Binary b1=new Binary();
b1.number=first;
if(b1.check_B())
{
int temp=b1.convertToDecimal();
Decimal d1=new Decimal();
d1.number=""+temp;
int ans=d1.convertToOctal();
System.out.println("The result is *** "+ans);
}else return;
}
public static void O2B(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Octal o1=new Octal();
o1.number=first;
if(o1.check_Oc())
{
int temp=o1.convertToDecimal();
Decimal d1=new Decimal();
d1.number=""+temp;
int ans=d1.convertToBinary();
System.out.println("The result is *** "+ans);
}else return;
}
public static void Quit(){
System.out.println("Thank you for ysing our system....");
}
}
public class Test{
static Scanner input=new Scanner(System.in);
public static void main(String arg[]){
int ch=0;
do{
System.out.println("**********************************************************************");
System.out.println("1.Decimal 2.Binary 3.Octal 4.Conversions 5.Quit");
System.out.println("**********************************************************************");
System.out.print("SHOOSE YOUR SYSTEM: ");
ch=input.nextInt();
switch(ch){
case 1:Decimal();break;
case 2:Binary();break;
case 3:Octal();break;
case 4:Conversions();break;
case 5:Quit();break;
}
}while(ch!=5);
}
public static void Decimal(){
char c='0';
System.out.println("\t\t A) Add two Decimal");
System.out.println("\t\t S) Subtract two Decimal");
System.out.println("\t\t M) Multiple two Decimal");
System.out.println("\t\t D) Divide two Decimal");
System.out.println("\t\t B) Back to main menu");
do{
System.out.println("Enter your formula:");
System.out.print("## ");
String in=input.next();
c=in.charAt(0);
switch(c){
case 'A':adddecimal();break;
case 'S':subdecimal();break;
case 'M':muldecimal();break;
case 'D':divdecimal();break;
case 'B':System.out.println("Finished section............");
}
}while(c!='B');
}
public static void adddecimal(){
String first=input.next();
String second=input.next();
Decimal d1=new Decimal();
Decimal d2=new Decimal();
d1.number=first;
d2.number=second;
if(d1.check_D()&&d2.check_D()){
int ans=d1.SumTwoDec(d2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void subdecimal(){
String first=input.next();
String second=input.next();
Decimal d1=new Decimal();
Decimal d2=new Decimal();
d1.number=first;
d2.number=second;
if(d1.check_D()&&d2.check_D()){
int ans=d1.SubTwoDec(d2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void muldecimal(){
String first=input.next();
String second=input.next();
Decimal d1=new Decimal();
Decimal d2=new Decimal();
d1.number=first;
d2.number=second;
if(d1.check_D()&&d2.check_D()){
int ans=d1.MultiTwoDec(d2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void divdecimal(){
String first=input.next();
String second=input.next();
Decimal d1=new Decimal();
Decimal d2=new Decimal();
d1.number=first;
d2.number=second;
if(d1.check_D()&&d2.check_D()){
int ans=d1.DivTwoDec(d2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void Binary(){
char c='0';
System.out.println("\t\t A) Add two binary");
System.out.println("\t\t S) Subtract two binary");
System.out.println("\t\t M) Multiple two binary");
System.out.println("\t\t D) Divide two binary");
System.out.println("\t\t B) Back to main menu");
do{
System.out.println("Enter your formula:");
System.out.print("## ");
String in=input.next();
c=in.charAt(0);
switch(c){
case 'A':addbinary();break;
case 'S':subbinary();break;
case 'M':mulbinary();break;
case 'D':divbinary();break;
case 'B':System.out.println("Finished section............");
}
}while(c!='B');
}
public static void addbinary(){
String first=input.next();
String second=input.next();
Binary b1=new Binary();
Binary b2=new Binary();
b1.number=first;
b2.number=second;
if(b1.check_B()&&b2.check_B()){
int ans=b1.SumTwoBinary(b2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void subbinary(){
String first=input.next();
String second=input.next();
Binary b1=new Binary();
Binary b2=new Binary();
b1.number=first;
b2.number=second;
if(b1.check_B()&&b2.check_B()){
int ans=b1.SubTwoBinary(b2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void mulbinary(){
String first=input.next();
String second=input.next();
Binary b1=new Binary();
Binary b2=new Binary();
b1.number=first;
b2.number=second;
if(b1.check_B()&&b2.check_B()){
int ans=b1.MultiTwoBinary(b2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void divbinary(){
String first=input.next();
String second=input.next();
Binary b1=new Binary();
Binary b2=new Binary();
b1.number=first;
b2.number=second;
if(b1.check_B()&&b2.check_B()){
int ans=b1.DivTwoBinary(b2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void Octal(){
char c='0';
System.out.println("\t\t A) Add two Octal");
System.out.println("\t\t S) Subtract two Octal");
System.out.println("\t\t M) Multiple two Octal");
System.out.println("\t\t D) Divide two Octal");
System.out.println("\t\t B) Back to main menu");
do{
System.out.println("Enter your formula:");
System.out.print("## ");
String in=input.next();
c=in.charAt(0);
switch(c){
case 'A':addOctal();break;
case 'S':subOctal();break;
case 'M':mulOctal();break;
case 'D':divOctal();break;
case 'B':System.out.println("Finished section............");
}
}while(c!='B');
}
public static void addOctal(){
String first=input.next();
String second=input.next();
Octal o1=new Octal();
Octal o2=new Octal();
o1.number=first;
o2.number=second;
if(o1.check_Oc()&&o2.check_Oc()){
int ans=o1.SumTwoOctal(o2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void divOctal(){
String first=input.next();
String second=input.next();
Octal o1=new Octal();
Octal o2=new Octal();
o1.number=first;
o2.number=second;
if(o1.check_Oc()&&o2.check_Oc()){
int ans=o1.DivTwoOctal(o2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void subOctal(){
String first=input.next();
String second=input.next();
Octal o1=new Octal();
Octal o2=new Octal();
o1.number=first;
o2.number=second;
if(o1.check_Oc()&&o2.check_Oc()){
int ans=o1.SubTwoOctal(o2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void mulOctal(){
String first=input.next();
String second=input.next();
Octal o1=new Octal();
Octal o2=new Octal();
o1.number=first;
o2.number=second;
if(o1.check_Oc()&&o2.check_Oc()){
int ans=o1.MultiTwoOctal(o2);
System.out.println("The result is *** "+ans);
}else return;
}
public static void Conversions(){
int c=0;
System.out.println("\n This section is designed to convert from one system to another.....\n");
System.out.println("1- Decimal to Binary\t3- Decimal to Octal\t5- Binary to Octal");
System.out.println("2- Binary to Decimal\t4- Octal to Decimal\t6- Octal to Binary");
System.out.println("\t\t 7- Back to Main");
System.out.println("****************************************************************************");
do{
System.out.print("\nENTER YOUR CHOICE: ");
c=input.nextInt();
switch(c){
case 1:D2B();break;
case 2:B2D();break;
case 3:D2O();break;
case 4:O2D();break;
case 5:B2O();break;
case 6:O2B();break;
case 7:System.out.println("Finished section............");
}
}while(c!=7);
}
public static void D2B(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Decimal d1=new Decimal();
d1.number=first;
if(d1.check_D())
{
int ans=d1.convertToBinary();
System.out.println("The result is *** "+ans);
}else return;
}
public static void B2D(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Binary b1=new Binary();
b1.number=first;
if(b1.check_B())
{
int ans=b1.convertToDecimal();
System.out.println("The result is *** "+ans);
}else return;
}
public static void D2O(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Decimal d1=new Decimal();
d1.number=first;
if(d1.check_D())
{
int ans=d1.convertToOctal();
System.out.println("The result is *** "+ans);
}else return;
}
public static void O2D(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Octal o1=new Octal();
o1.number=first;
if(o1.check_Oc())
{
int ans=o1.convertToDecimal();
System.out.println("The result is *** "+ans);
}else return;
}
public static void B2O(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Binary b1=new Binary();
b1.number=first;
if(b1.check_B())
{
int temp=b1.convertToDecimal();
Decimal d1=new Decimal();
d1.number=""+temp;
int ans=d1.convertToOctal();
System.out.println("The result is *** "+ans);
}else return;
}
public static void O2B(){
System.out.print("Write mumber to convert: ");
String first=input.next();
Octal o1=new Octal();
o1.number=first;
if(o1.check_Oc())
{
int temp=o1.convertToDecimal();
Decimal d1=new Decimal();
d1.number=""+temp;
int ans=d1.convertToBinary();
System.out.println("The result is *** "+ans);
}else return;
}
public static void Quit(){
System.out.println("Thank you for ysing our system....");
}
}
تحياتي
جربت استخدام الكود ولم يشتغل طلع عندي اخطاء في استخدام if الاولي و for التي تليها
ردحذف