I'm new to Java programming and am really enjoying it. I made a bitshift program, but I think it's too long.
I expect the program to:
- Let users input the number and degree to be shifted
- Specify if it is a right or left shift
- Restart the program if the user requires at the end
- Ensure exceptions don't crash the program
import java.util.Scanner;
class BitShift{
int a,b;
String c;
static int shiftNo(){
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
return x;
}
static String shiftDir(){
Scanner sc=new Scanner(System.in);
String x=sc.nextLine();
return x;
}
}
class BitShiftObjects1 extends BitShift{
public static void main(String []args){
int answer;
BitShift ob1=new BitShift();
boolean isValue=false;
while(isValue==false){
try{
System.out.println("Please enter number to be shifted");
ob1.a=shiftNo();
System.out.println("Please enter degree to be shifted");
ob1.b=shiftNo();
isValue=true;
}
catch(java.util.InputMismatchException e){
System.out.println("ERROR:**Please Input Integer Number**");
System.out.println("**Please enter from begining**");
System.out.println("");
}
}
isValue=false;
while(isValue==false){
System.out.println("Please enter r or l");
ob1.c=shiftDir();
if(ob1.c.equals("r") || ob1.c.equals("l"))
isValue=true;
else
System.out.println("ERROR:**Please enter LOWERCASE r/l**");
}
if(ob1.c.equals("r")){
answer=ob1.a >> ob1.b;
System.out.println("Your Answer is : "+answer);
}
else{
answer=ob1.a << ob1.b;
System.out.println("Your Answer is : "+answer);
}
System.out.println("");
System.out.println("Do you want to enter a new data set??");
System.out.println("Press y or n");
isValue=false;
while(isValue==false){
ob1.c=shiftDir();
if(ob1.c.equals("y")){
System.out.println("**Reinitializing program**");
isValue=true;
main(args);
}
else if(ob1.c.equals("n"))
isValue=true;
else
System.out.println("ERROR:**Please enter lowercase y or n**");
}
}
}