The Artima Developer Community
Sponsored Link

Java Answers Forum
java program problem..

4 replies on 1 page. Most recent reply: Aug 19, 2009 12:17 AM by litu sahoo

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Ian Ebrole

Posts: 3
Nickname: christoian
Registered: Apr, 2006

java program problem.. Posted: Apr 26, 2006 1:20 AM
Reply to this message Reply
Advertisement
hi guys.. i have a problem of my program in java. AND THIS IS NOW THE PROBLEM: Hope you can make me a simple program.god bless..
PROBLEM:
"Given two bit strings of length n, find the bitwise AND, bitwise OR, and bitwise XOR of these strings, using METHOD."
Ex. input bitwise string 1 : 010111
input bitwise string 2 : 101111

Output: The bitwise AND of string 1 and string 2 is 000111.
The bitwise OR of string 1 and string 2 is 111111.
The bitwise XOR of string 1 and string 2 is 111000.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: java program problem.. Posted: Apr 26, 2006 11:53 PM
Reply to this message Reply
1. create a empty String

2. Create a loop from 0 to n-1 (I assuem you use i as a )
2.1 Read char at position i of each of the input strings
2.2 Check if the chars are either '1' or '0' otherwise exit
2.3 create two boolean variables, set them to true or false depending on the char.
2.4 Combine the variables.
2.5 Add '1' or '0' to the empty String, depending on the previous result.
3. return the String

Ian Ebrole

Posts: 3
Nickname: christoian
Registered: Apr, 2006

Re: java program problem.. Posted: Apr 27, 2006 5:35 PM
Reply to this message Reply
hi! can you make me a program of my problem? please... im beaging you... i will passed this on may 2, 06..please help me..thank you ..god bless you...

M Adawi

Posts: 9
Nickname: adawi
Registered: Mar, 2006

Re: java program problem.. Posted: May 1, 2006 4:17 AM
Reply to this message Reply
import java.io.*;

/*
Notes:

-Read Long.parseLong(String s,radix) from java API.

-Remeber that int (signed integer)
is in range negative 2^(to the power)31
and positive 2^32.

-Solution of Matthias Neumair is more correct and much
more faster because I used Long.parseLong which will convert
a String of (bits) to long which need alot of calculations
with alot of cpu consuming multiplications, then reconvert
long to BinaryString which also supose to be cpu consuming

-I think you will got C if you write this program as it. so
update it as Matthias Neumair told you.

-Best Wishes Mohammed Eladawi
*/


class Bitwise {
public static String and (String s1, String s2) {
return Long.toBinaryString(Long.parseLong(s1,2) & Long.parseLong(s2,2));
}

public static void main (String arg[]) throws Exception {
DataInputStream in1 = new DataInputStream (System.in);
DataInputStream in2 = new DataInputStream(System.in);
byte[] input1 = new byte[34];
byte[] input2 = new byte[34];

System.out.print("Input1>>");
int count1 = in1.read(input1);

System.out.print("Input2>>");
int count2 = in2.read(input2);

String inputS1;
String inputS2;

if (count1 > 2)
inputS1 = new String(input1 , 0 , count1 - 2);
else
inputS1 = new String("0");

if (count2 > 2)
inputS2 = new String(input2 , 0 , count2 - 2);
else
inputS2 = new String("0");

System.out.println(inputS1 + "\n and \n" + inputS2 + "\n = \n" + (and (inputS1 , inputS2)));

in1.close();
in2.close();
}
}

litu sahoo

Posts: 2
Nickname: litucet08
Registered: Aug, 2009

Re: java program problem.. Posted: Aug 19, 2009 12:17 AM
Reply to this message Reply
I think this'll help you.

import java.io.*;
public class Str {

int x=0;
int y=0;
int i=0;
char first;
char second;
String rev_result="";



String cal_and(String a,String b){
int max_len=0;


if(a.length()>=b.length())
max_len=a.length();
else
max_len=b.length();


for(i=0;i<max_len;i++){
if(i<a.length()){
first=a.charAt(i);
}
if(i<b.length()){
second=b.charAt(i);
}
if(first=='1' && second=='1'){

rev_result=rev_result.concat("1");

}
else{
rev_result=rev_result.concat("0");
}

}


return rev_result;

}




public static void main(String[] args){
String a="";
String b="";
Str obj=new Str();
System.out.println("please enter first string in byte format:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try{
a=br.readLine();
}
catch(IOException io){
System.out.println("An Exception Has Occured");
System.exit(1);
}
System.out.println("please enter second string in byte format:");
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
try{
b=br2.readLine();
}
catch(IOException io){
System.out.println("An Exception Has Occured");
System.exit(1);
}



String result=obj.cal_and(a,b);
System.out.print("result="+result);

}

}

Flat View: This topic has 4 replies on 1 page
Topic: Audio streams Previous Topic   Next Topic Topic: Threads for calling procedures

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use