Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am a COBOL programmer, desperately trying to learn and program Java. Although I understand the OO priciples and concepts, I am at a lost putting all together.

I am trying to find some examples on how code will look in COBOL vs Java - something a bit more complicated than "hello world" [I am past that initial headache! :-)], that includes OO principles - basically, a decent 1000+ liner in COBOL, with the Java code to work through.

After working through book after book, doing the examples, and even writing small java programs at work, I think working through an example I can compare will be the best way to getting my thinking cap converted, and being able to do in Java what I can do after 10 years with COBOL.

Can anybody help?

share|improve this question

closed as not constructive by gnat, Walter, Glenn Nelson, MichaelT, GlenH7 Feb 19 '13 at 14:44

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

4  
It's a bad, bad idea to try and compare Java with COBOL. Why? They're actually different languages with very little in common. There are -- perhaps -- millions of lines of Java available on line. What's wrong with them? Sourceforge, for example, has hundreds of Java projects. The Apache foundation has dozens and dozens. What's wrong with all of that Java code? Why isn't it good enough for you to learn from? Please be specific. Try to list a specific Java project and why the code isn't helpful. –  S.Lott Oct 19 '11 at 2:56
    
    
Have you already tried to implement one of your cobol programs in another language? You could try another procedural language like C first. –  ott-- Feb 19 '13 at 14:02

2 Answers 2

Here's a good example, from this page. It computes a 25 year investment based on a particular interest rate which is given as the yearly interest rate but actually computed and added to the balance each day.

IN COBOL:

   program-id. Compare.

   working-storage section.
   01 yield-exact      pic 9(9)v99 comp.
   01 yield-float      pic 9(9)v99 comp.
   01 money            pic 9(9).99.

   01 yield-info.
     03 start-balance      pic 9(9)v9(2)  comp.
     03 yield              pic 9(9)v9(2)  comp.
     03 interest           pic     v9(7)  comp.
     03 days               pic 9(9)       comp.
     03 years              pic 9(2)       comp.   

   procedure division.
       move 0.0625 to interest
       move 9132   to days
       move 25     to years
       perform varying start-balance from 1000000 by 0.01
                            until start-balance = 1000100

           call "get-yield-exact" using yield-info
           move yield to yield-exact

           call "get-yield-float" using yield-info
           move yield to yield-float

           if yield-float not = yield-exact 
                   move start-balance to money
                   display "For start balance: " money
                   move yield-exact to money
                   display " - At 25 years exact balance is " money
                   move yield-float to money 
                   display " - But floating point is        " money
                   display " "
           end-if

       end-perform
       display "All Done"

       goback.

   end program Compare.


  * Calculate the yield using exact mathematics
   program-id. get-yield-exact.

   working-storage section.

   01 current-balance   pic 9(9)v9(18) comp.
   01 interest-c        pic 9(9)v9(18) comp.
   01 intermediate-7    pic 9(9)v9(7)  comp.
   01 calc-day          pic 9(9)       comp.

   linkage section.
   01 yield-info.
     03 start-balance      pic 9(9)v9(2)  comp.
     03 yield              pic 9(9)v9(2)  comp.
     03 interest           pic     v9(7)  comp.
     03 days               pic 9(9)       comp.
     03 years              pic 9(2)       comp.      

   procedure division using yield-info.

       move    start-balance to current-balance
       move    interest to interest-c
       compute interest-c rounded = (interest-c * years) / days
       compute intermediate-7 rounded = interest-c
       move    intermediate-7 to interest-c
       move    days to calc-day

       perform varying calc-day from 1 by 1 until calc-day greater days

           compute intermediate-7 rounded
                   = current-balance + (current-balance * interest-c) 
           move    intermediate-7 to current-balance         

       end-perform
       move current-balance to yield

   end program get-yield-exact.

  * Calculate the yield using floating point mathematics
   program-id. get-yield-float.

   working-storage section.

   01 current-balance   float-long.
   01 interest-c        float-long.
   01 intermediate-7    pic 9(9)v9(7) comp.
   01 calc-day          pic 9(9)      comp.

   linkage section.
   01 yield-info.
     03 start-balance      pic 9(9)v9(2)  comp.
     03 yield              pic 9(9)v9(2)  comp.
     03 interest           pic     v9(7)  comp.
     03 days               pic 9(9)       comp.
     03 years              pic 9(2)       comp.      

   procedure division using yield-info.

       move    start-balance to current-balance
       move    interest to interest-c
       compute interest-c rounded = (interest-c * years) / days
       compute intermediate-7 rounded = interest-c
       move    intermediate-7 to interest-c
       move    days to calc-day

       perform varying calc-day from 1 by 1 until calc-day greater days

           compute intermediate-7 rounded
                   = current-balance + (current-balance * interest-c) 
           move    intermediate-7 to current-balance         

       end-perform
       move current-balance to yield

   end program get-yield-float.

In Java:

import java.math.BigDecimal;
import java.math.RoundingMode;

public class GetYield 
{

 public static BigDecimal computeYield
 (
   BigDecimal startBalance,
   BigDecimal interest,
   int days,
   int years
    )
 {
  int large=18;
  int small=7;
  RoundingMode rm=RoundingMode.HALF_UP;
  BigDecimal currentBalance=startBalance.setScale(large,rm);
  BigDecimal interestC=interest.setScale(large,rm);
  interestC=
   (interestC.multiply(BigDecimal.valueOf(years,large)))
   .divide(BigDecimal.valueOf(days,large),rm)
   .setScale(small,  rm)
   .setScale(large, rm);
  System.out.println(interestC);
  for(int calcDay=1;calcDay<=days;++calcDay)
  {
   currentBalance=
    (currentBalance.add(currentBalance.multiply(interestC).setScale(large, rm))).
    setScale(small,  rm).
    setScale(large, rm);

  }

  return currentBalance.setScale(2, rm);
 }

 public static void main(String[] args) 
 {
  BigDecimal yield=computeYield(BigDecimal.valueOf(1000095.20),BigDecimal.valueOf(0.0625),9132,25);
  System.out.println(yield);
 }

}
share|improve this answer
    
Note that for money you can frequently get along with long for cents instead of BigDecimal. –  user1249 Oct 19 '11 at 6:28
    
Counting cents breaks when dealing with the handful of currencies in North Africa and the Middle East that are divided into thousandths. –  Blrfl Oct 19 '11 at 12:18
2  
For best results with regards to currency, create a Currency type, which will then either throw errors or convert, when adding two disparate currencies. 500 USD is not equivalent to 500 Yen; keeping a 'money' amount as a primitive is asking for somebody to use it wrong somewhere. –  Clockwork-Muse Oct 19 '11 at 16:33

My advice, after 20 years of COBOL, is to learn VB.NET rather than Java. The language syntax is much, much closer to COBOL so it should make for an easier transition.

share|improve this answer
2  
hahaha... No. A lot of popular languages have a C-based syntax. If he's going to make the effort of moving from COBOL, he might as well jump into something that will make it more "seamless" to expand his horizons. VB looks only like VB. –  MetalMikester Mar 30 '12 at 10:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.