Tampilkan postingan dengan label java. Tampilkan semua postingan
Tampilkan postingan dengan label java. Tampilkan semua postingan

Round robin java code

Posted by Unknown Senin, 02 Desember 2013 0 komentar
ROUND ROBIN java code

import java.util.*;
class Process
{ String name;
 int at,st;
 int rt;
Process(String n,int a,int s) 
{ name=n; 
 at=a;
 st=s;

 rt=st;
 }
}
class RoundRobin 
{ public static void main(String arg[]) 
  { Scanner sc=new Scanner(System.in);
     System.out.println("enter the no. processes : ");
     int n=sc.nextInt();
     Process p[]=new Process[n];
     System.out.println("enter Process details : ");
     for(int i=0;i<n;i++)  
     { System.out.println("process : "+(i+1)+" : \n enter name of process ");
       String s=sc.next();
       System.out.println("enter arival time : ");
       int a=sc.nextInt();
       System.out.println("enter service time : ");
       int st=sc.nextInt();
       p[i]=new Process(s,a,st);
      }
     System.out.println("name \t AT \t ST ");
     for(int i=0;i<n;i++)
        System.out.println(p[i].name+" \t "+p[i].at+" \t "+p[i].st);
     Vector<Process> v=new Vector<Process>();
     v.setSize(1000);
     for(int i=0;i<n;i++)
      v.insertElementAt(p[i],p[i].at);
     int t=0,i=0;
     
     while(i<=t)
     {
        try
        {
         if(v.elementAt(i).rt<=2)
          { System.out.println("process : "+v.elementAt(i).name+" from t= "+t+" to ");
             t=t+v.elementAt(i).rt;
            System.out.println("t= "+t);
            v.remove(i);
           }
           else
           {System.out.println("process : "+v.elementAt(i).name+" from t= "+t+" to ");
             t=t+2;
             v.elementAt(i).rt=v.elementAt(i).rt-2;
             System.out.println("t= "+t);
             
             v.insertElementAt(v.elementAt(i),t-1);
             
             
            }
            i++;
         }
         catch(Exception e)
         {
            i++;
          }
         
           
       }      
             
             
             
     }
   }

                                       OUTPUT
Enter the no. processes :6

enter Process details :
process : 1 :
enter name of process  A
enter arival time : 0
enter service time : 5
process : 2 :
enter name of process  B
enter arival time : 3
enter service time : 6
process : 3
enter name of process  C
enter arival time : 5
enter service time : 2
process : 4
enter name of process  D
enter arival time : 7
enter service time : 3
process : 5
enter name of process  E
enter arival time : 7
enter service time : 5
process : 6
enter name of process F
enter arival time : 9
enter service time : 4

name     AT      ST
A        0       5
B        3       6
C        5       2
D        7       3
E        7       5
F        9       4

process : A from t= 0 to t= 2
process : A from t= 2 to t= 4
process : A from t= 4 to t= 5
process : B from t= 5 to t= 7
process : B from t= 7 to t= 9
process : C from t= 9 to t= 11
process : E from t= 11 to t= 13
process : D from t= 13 to t= 15
process : F from t= 15 to t= 17
process : E from t= 17 to t= 19
process : D from t= 19 to t= 20
process : F from t= 20 to t= 22
process : E from t= 22 to t= 23



Enjoy !

Baca Selengkapnya ....

Internet checksum program java

Posted by Unknown 0 komentar
import java.util.*; 
 class InternetChecksum
 {
 public static void main (String args[])
{
Scanner scan = new Scanner (System.in);
 System.out.println("Enter the string input:"); 
 String input = scan.next();
 int checksum = generateChecksum(input); 
 System.out.println("The checksum generated is "+Integer.toHexString(checksum));
System.out.println("Enter the data to be sent:"); 
 input = scan.next(); 
 System.out.println("Enter the checksum to be sent:"); 
 checksum = Integer.parseInt((scan.next()),16); 
 receive(input,checksum);
 } 

 static int generateChecksum(String s) 
{
 String hex_value = new String();
 int x,i,checksum=0; 
 for(i=0; i <s.length()-2; i=i+2)
{
x = (int)(s.charAt(i));
 hex_value = Integer.toHexString(x); 
 x = (int)(s.charAt(i+1)); 
 hex_value = hex_value + Integer.toHexString(x);
System.out.println(s.charAt(i)+""+s.charAt(i+1)+":"+hex_value); 
 x=Integer.parseInt(hex_value,16); 
 checksum+=x;
 } 

 if(s.length()%2==0)
{
x = (int)(s.charAt(i)); 
 hex_value = Integer.toHexString(x); 
 x = (int)(s.charAt(i+1)); 
 hex_value = hex_value + Integer.toHexString(x);
System.out.println(s.charAt(i)+""+s.charAt(i+1)+":"+hex_value); 
 x=Integer.parseInt(hex_value,16);
checksum+=x; 
 } 

 else
{
x =(int)(s.charAt(i)); 
 hex_value= "00" + Integer.toHexString(x); 
 x=Integer.parseInt(hex_value,16); 
 System.out.println(s.charAt(i) + " : " + hex_value);
 } 

 checksum+=x;
 hex_value=Integer.toHexString(checksum); 
 if(hex_value.length()>4)
{
int carry = Integer.parseInt(""+hex_value.charAt(0),16); 
 hex_value=hex_value.substring(1,5); 
 checksum=Integer.parseInt(hex_value,16); 
 checksum+=carry; 
 } 

 checksum = generateComplement(checksum);
 return checksum;
 }

 static void receive (String s, int checksum)
{ 
 int generated_Checksum = generateChecksum(s); 
 generated_Checksum = generateComplement(generated_Checksum); 
 int syndrome = generated_Checksum+checksum; 
 syndrome = generateComplement(syndrome); 
 System.out.println("Syndrome = " + Integer.toHexString(syndrome)); 

 if(syndrome == 0) 
{
System.out.println("Data is received without error.");
 }
 else 
{
System.out.println("There is an error in the received data.");
 }
 } 

 static int generateComplement (int checksum)
{
checksum = Integer.parseInt("FFFF",16) - checksum;
return checksum;
} 

}


Happy coding!! 
Enjoy!

Baca Selengkapnya ....

Tips and guidelines for coding program

Posted by Unknown Senin, 14 Oktober 2013 0 komentar
Here is a list of tips which may help us while we code any piece of program. For a long time, I wanted to sum them up and keep it as a checklist.




Commit Messages

Make sure you put a commit message which makes sense. It is true that a good commit message might be difficult to come up with, but please avoid messages like “Fixed bug” or “Rendering”. Which bug did you fix? What is the bug number? What is the bug about? What rendering? What did you do with rendering? Did you fix it? Did you add rendering capability? Did you remove rendering capability?
It is a good practice to add a line of commit message title, followed by a single blank line and then an optional detailed description of the change which might not be obvious. If you made a commit which alters the working or behavior of the component, you might want to explain why you took that decision in the description section of the commit message

Check In Early, Check In Often

Jeff Atwood has an excellent post on this recommendation. My personal motto is to check-in when I am done for the day and pull in the changes when I start working for the day. I have a friend whose motto was “Every morning, you wake up and do ‘svn up’, even if you haven’t brushed your teeth”.


Keep the change-set manageable

Avoid any such scenario where your commit diff is 2000 lines unless you are nuking a component or any special scenarios. If the component you are working on enters the source tree in a single commit with 4000 lines of diff after 5 days, then STOP. THINK. Is this really the correct way? Can you do it in a better way?
If your single diff contains truck load of different features with 25 files affected, then it is a exercise in frustration for anyone who is attempting to review it. Try breaking it down such that you are able to follow“Check In Early, Check In Often”.

Learning to resolve merge conflicts

It isn’t always easy to fix it, but don’t forget that merge conflicts do happen and you will need to deal with it even if you collaborate on a piece of software in a very smart manner.

Get your code reviewed

No one’s code is perfect. At times, we are insecure of our coding abilities and become overly protective of our code quality.
Even if you don’t have a better programmer around, get it reviewed by your peers whom you consider at par with you.
Try reviewing code, that helps too – you learn better ways of implementing features and ideas.
If someone criticizes your code, resist from taking it personally. Usually the person criticizing your code has got nothing against you.

Start Questioning

You don’t need to catch your team lead by the neck and demand the answer for choosing this method. Question yourself! Think over it. Try again!
A spark of inquisitiveness goes a long way.

Baca Selengkapnya ....

Is it possibleto have GCC, G++ , Java on UBUNTU?

Posted by Unknown Jumat, 20 September 2013 0 komentar
In our college , K J Somaiya college of Engineering we have started using UBUNTU for all of our practical sessions.

Since all of us are new to this everyone had a doubt and facing difficulty in performing programs on this.
This tutorial is not written from scratch.
I would like to thank all people who
enlightened me about Linux and Open source.
This tutorial acknowledges the help of Ubuntu Community (Docs) and people at online forums who cleared
my doubts on compilers.





Installing compilers is one of the easiest things there out at Linux. It
may seem a bit tough and complicated in the beginning but apt-get
and Synaptic Package Manager simplify your work a lot. As a newbie
developer you would require a C/C++ and a Java compiler. Installing
them is a matter of few commands.
The GNU C and GNU C++ Compiler:
The GNU C compiler is also called gcc and C++ is called g++. You
may also require the GNU make utility. All of them can get installed
by getting the build-essential package.

Just use this command:
sudo apt-get install build-essential

The GNU Java Compiler:
Firstly note that this compiler is not the one provided by Sun. The GNU Java compiler is named as gcj. Just install the package gcj
To install the GNU Java bytecode interpreter, install the gij package.
To have gcj compile to executables, install libgcj6-dev

Use the command:
sudo apt-get install gcj gij libgcj6-dev

Now the question arises that many people get :

 Errors like “build-essential not found” are displayed.

Here is an alternate way of doing the same:
Most people want to use linux in GUI mode just like windows. If this the case then why use the terminal? Just open the Synaptic Package Manager and search for the package gcc. Select the package, some more packages will be highlighted and will be asked to also be included in the install process. These are dependencies. Select them also and click on Apply. The packages are download and installed. To install g++,make and gcj just do the same as above mentioned for gcc package.

Now it would be working.!!

Baca Selengkapnya ....

Job Sequencing with Deadlines (Java program)

Posted by Unknown 0 komentar




Let us today understand the concept of

                   Job Sequencing with Deadlines.

The problem is stated as below.
•There are n jobs to be processed on a machine.
•Each job i has a deadline di≥ 0 and profit pi≥0 .
•Pi is earned iff the job is completed by its deadline.
•The job is completed if it is processed on a machine for unit time.
•Only one machine is available for processing jobs.
Only one job is processed at a time on the machine


•A feasible solution is a subset of jobs J such that each job is completed by its deadline.
•An optimal solution is a feasible solution with maximum profit value.
  Lets take an example to make the things more clear.

 Let n = 4, (p1,p2,p3,p4) = (100,10,15,27), (d1,d2,d3,d4) = (2,1,2,1)




Now once we have understood the concept of Job Sequencing here goes the Java Program for the same.



/*********** JOB SEQUENCING WITH DEADLINES  by:" AKHIL KILLAWALA"*****************/   
// PROGRAM:
import java.util.*;
class job
{
int p;    //.............for profit of a job
int d;    //.............for deadline of a job
int v;    //.............for checking if that job has been selected
/*************default constructor**************/
job()
{
p=0;
d=0;
v=0;
}
job(int x,int y,int z) // parameterised constructor
{
p=x;
d=y;
v=z;
}
}
class js
{
static int n;
static int out(job jb[],int x)
{
for(int i=0;i<n;++i)
if(jb[i].p==x)
return i;
return 0;
}
public static void main(String args[])
{
Scanner scr=new Scanner(System.in);
System.out.println("Enter the number of jobs");
n=scr.nextInt();
int max=0;   // this is to find the maximum deadline
job jb[]=new job[n];
/***********************Accepting job from user*************************/
for(int i=0;i<n;++i)
{
System.out.println("Enter profit and deadline(p d)");
int p=scr.nextInt();
int d=scr.nextInt();
if(max<d)
max=d;     // assign maximum value of deadline to "max" variable
jb[i]=new job(p,d,0);  //zero as third parameter to mark that initially it is unvisited
}
//accepted jobs from user

/*************************Sorting in increasing orser of deadlines*************************/
for(int i=0;i<=n-2;++i)
{
 for(int j=i;j<=n-1;++j)
 {
   if(jb[i].d>jb[j].d)
   {
     job temp=jb[i];
     jb[i]=jb[j];
     jb[j]=temp;
   }
 }
}
// sorting process ends

/******************************Displaying the jobs to the user***********************/
System.out.println("The jobs are as follows ");
for(int i=0;i<n;++i)
System.out.println("Job "+i+" Profit = "+jb[i].p+" Deadline = "+jb[i].d);
// jobs displayed to the user

int count;
int hold[]=new int[max];
for(int i=0;i<max;++i)
hold[i]=0;
/*****************************Process of job sequencing begins*************************/
for(int i=0;i<n;++i)
{
count=0;
  for(int j=0;j<n;++j)
  {
   
   if(count<jb[j].d && jb[j].v==0 && count<max && jb[j].p>hold[count])
   {
     int ch=0;
    
  if(hold[count]!=0)
     {
  ch=out(jb,hold[count]);
     jb[ch].v=0;
  }
 
  hold[count]=jb[j].p;
  jb[j].v=1;
     ++count;
 
  } // end of if
 
  } //end of inner for
}// end of outer for
/******************************job sequencing process ends******************************/

/******************************calculating max profit**********************************/
int profit=0;
for(int i=0;i<max;++i)
profit+=hold[i];
System.out.println("The maximum profit is "+profit);

}//end main method
}//end class

/****************SAMPLE OUTPUT *********************/
/*
Enter the number of jobs
4
Enter profit and deadline(p d)
70 2
Enter profit and deadline(p d)
12 1
Enter profit and deadline(p d)
18 2
Enter profit and deadline(p d)
35 1
The jobs are as follows
Job 0 Profit = 12 Deadline = 1
Job 1 Profit = 35 Deadline = 1
Job 2 Profit = 18 Deadline = 2
Job 3 Profit = 70 Deadline = 2

The maximum profit is 105

*/








Baca Selengkapnya ....
Trik SEO Terbaru support Online Shop Baju Wanita - Original design by Bamz | Copyright of Android reverb.