0% found this document useful (0 votes)
569 views6 pages

3 4 5 Rule Discretization

The document defines a Java class that takes user input for a minimum and maximum range. It then takes input for low and high values within that range. It performs rounding on the low and high values based on their most significant digit using a 3-4-5 rounding rule to generate ranges between the rounded low and high values. It outputs the rounded minimum and maximum ranges as well as the generated ranges.

Uploaded by

TarunKhandelwal1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
569 views6 pages

3 4 5 Rule Discretization

The document defines a Java class that takes user input for a minimum and maximum range. It then takes input for low and high values within that range. It performs rounding on the low and high values based on their most significant digit using a 3-4-5 rounding rule to generate ranges between the rounded low and high values. It outputs the rounded minimum and maximum ranges as well as the generated ranges.

Uploaded by

TarunKhandelwal1
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

import java.util.

*;
public class finalh
{
public static void main(String args[])
{
finalh h=new finalh();
int minrange=0;
int maxrange=0;
int low=0;
int high=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Range");
boolean a=true;
///////INPUT FOR TOP LEVEL ELEMENTS//////////
while(a)
{
System.out.println("Enter Min Range");
minrange=sc.nextInt();
System.out.println("Enter Max Range");
maxrange=sc.nextInt();
if(minrange<maxrange)
{
a=false;
}
}
a=true;
////////////INPUT FOR LOW AND HIGH VALUE////////////////
while(a)
{
System.out.println("Enter value For Low");
low=sc.nextInt();
System.out.println("Enter value For High");
high=sc.nextInt();
if(low>minrange && high<maxrange)
{
a=false;
}
}
/////////NO OF DIGITS//////////////
int ihigh=h.digit(high);
int jlow=h.digit(low);
int dig=0;
if(ihigh>jlow)
{
dig=ihigh;
}
else
{
dig=jlow;
}
boolean flag=false;
///////////CHECKING FOR A NO IS ROUNDED OR NOT////////////////////
boolean checkhigh=h.chec(high,ihigh);
boolean checklow=h.chec(low,jlow);
//////////////CALCULATING MSB /////////////////////////
int lhigh=h.msb(high);
int klow=h.msb(low);
////////////////ROUNDEING NOS//////////////////////
if(low>0)
{
klow=h.round("min","positive",low, klow, dig, checklow);
}
else
{
klow=h.round("min","negative",low, klow, dig, checklow);
}
if(high>0)
{
lhigh=h.round("max","positive",high, lhigh, dig, checkhigh);
}
else
{
lhigh=h.round("max","negative",high, lhigh, dig, checkhigh);
}
if(low>0 && jlow<ihigh)
{
if(low<100)
{
klow=0;
}
else
{
klow=1;
while(ihigh>0)
{
klow=klow*10;
ihigh--;
}
klow=klow/10;
}
}
else if(low<0 && jlow<ihigh)
{
klow=-1;
while(ihigh>0)
{
klow=klow*10;
ihigh--;
}
klow=klow/10;
}
ihigh=h.digit(high);
if(high>0 && ihigh<jlow)
{
lhigh=1;
jlow--;
while(jlow>0)
{
lhigh=lhigh*10;
jlow--;
}
}
else if(high<0 && ihigh<jlow)
{
if(high>-100)
{
lhigh=0;
}
else
{
lhigh=-1;
jlow--;
while(jlow>0)
{
lhigh=lhigh*10;
jlow--;
}
}
}
////////////////PRINTTING RANGES////////////////
List<Integer> arr=new ArrayList<Integer>();
System.out.println("////////LEVEL ONE////////");
arr=h.rule(klow, lhigh);
int newmin=h.topround(minrange);
int newmax=h.topround(maxrange);
h.rule(newmin,klow);
h.rule(lhigh,newmax);
int no=arr.size();
no--;
System.out.println(".............LEVEL TWO.............");
for(int i=0;i<no;i++)
{
int arrmin=arr.get(i);
int arrmax=arr.get(i+1);
h.rule(arrmin,arrmax);
}
}

///////////////FUNCTION FOR ROUNDING TOP LEVEL VALUES///////////////


public int topround(int num)
{
int rounded=1;
finalh h=new finalh();
int dig=h.digit(num);
int msb=h.msb(num);
boolean check=h.chec(num,dig);
System.out.println(msb);
if(check)
{
rounded=num;
}
else
{
dig--;
if(num<0)
{
msb--;
}
else
{
msb++;
}
rounded=msb;
while(dig>0)
{
rounded=rounded*10;
dig--;
}
}
return rounded;
}
//////////////FUNCTION FOR 3-4-5 RULE/////////////
public ArrayList rule(int klow,int lhigh)
{
finalh h=new finalh();
int diff=lhigh-klow;
int diffmsb=h.msb(diff);
int val=0;
switch (diffmsb)
{
case 3:
case 6:
case 9:
case 7:
val=3;
break;
case 4:
case 2:
case 8:
val=4;
break;
case 1:
case 5:
case 0:
val=5;
break;
}
int val1=0;
val1=diff/val;
int range=klow;
System.out.println("///////Ranges///////////");
ArrayList arr=new ArrayList();
arr.add(range);
for(int i=1;i<=val;i++)
{
System.out.print(range+".....");
range=range+val1;
arr.add(range);
System.out.println(range);
}
return arr;
}

////////FUNCTION FOR CHECKING A NO. IS ROUNDED OR NOT/////////////////


public boolean chec(int a,int b)
{
boolean checks = false;
int n=0;
b--;
while(b>0)
{
n=a % 10;
if(n==0)
{
checks=true;
}
else
{
checks=false;
break;
}
a=a/10;
b--;
}
return checks;
}

////////////FUNCTION FOR CALCULATING NO OF DIGITS/////////////////


public int digit(int n)
{
int x=0;
while(n!=0)
{
n=n/10;
x++;
}
return x;
}
/////////////FUNCTION FOR CALCULATING MSB/////////////////
public int msb(int n1)
{
int n=n1;
if(n<0)
{
n=n*-1;
}
int x=0;
x=n/10;
while(x>10)
{
x=x/10;
}
if(x==10)
{
x=1;
}
if(n1<0)
{
x=x*-1;
}
return x;
}
//////////////FUNCTION FOR ROUNDING A NO.////////////////////////
public int round(String type,String casee,int no,int msb,int digit,boolean c
heck)
{
int x=0;
if(msb==9)
{
if((type.equals("max") && casee.equals("positive")) || (type.equals(
"min") && casee.equals("negative")))
{
msb=1;
while(digit>0)
{
msb=msb*10;
digit--;
}
}
else if((type.equals("max") && casee.equals("negative")) || (type.eq
uals("min") && casee.equals("positive")))
{
while(digit>0)
{
msb=msb*10;
digit--;
}
msb=msb/10;
}
}
else if(check)
{
//msb=no;
while(digit>0)
{
msb=msb*10;
digit--;
}
msb=msb/10;
}
else
{
if(type.equals("max") && casee.equals("positive"))
{
msb++;
}
else if(type.equals("min") && casee.equals("negative"))
{
msb--;
}
while(digit>0)
{
msb=msb*10;
digit--;
}
msb=msb/10;
}
x=msb;
return x;
}
}

You might also like