Computational Applications in Nuclear Astrophysics using Java Java course Lecture 3 Prepared for course 160410/411 Michael C. Kunkel m.kunkel@fz-juelich.de Materials taken from; docs.oracle.com Teach Yourself Java ISBN 9780078825705
Maven Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting and documentation from a central piece of information. https://maven.apache.org/
Maven We will use maven for our project dependancies Lets look at our pom file you have from the Github project
Java
Java
Methods in Java A method is a block of statements that are grouped together to perform an operation or specific task The equivalent of Java methods in C and C ++ are functions Methods are used to divide code into logical pieces Methods can return values, or just perform an operation
Class Work Create a package in Eclipse Name it thirdclass Create a public class named MyMethods Lets create a method called printhello that will take 2 arguments of String and int Example on next slide Create a main method call the printhello method
Java
Class Work Using the package thirdclass Using the public class named MyMethods Lets create a method called printstring that will take 0 arguments and return a String Example on next slide In the main method call the string value of printstring
Class Work
Method Overloading A class can have multiple methods having the same name but different parameters. This is known as method overloading Having the same method name that performs the same operation increases the readability of a program To overload a method Change the number of parameter Change the data type of of one parameter In Java it is not possible to overload a method by simply changing the return type.
Class Work Using the package thirdclass Using the public class named MyMethods Lets create a method called myoverload that will take 1 parameter Lets overload the method called myoverload 2 parameters " "3 parmeters Example on next slide In the main method call all 3 overloaded methods
Class Work
Class Work Using the package thirdclass Using the public class named OnlyGravitationalHeating Keep the mass and luminosity of the sun constant, write a method that will return what the radius of the sun would be today, if the only source of energy was gravitational heating Overload this method to take an integer as input and return the radius of the sun if it were to have enough heat for X*ageOfSun You may use the Constants package
Class Work Using the package thirdclass Using the public class named SemiEmpiricalMassFormula Write a method that returns the binding energy for parameters N and Z You may use the Constants package You may update the Constants package
Java Loops for loop A for loop is a looping stamens that allows you to execute a code block a specific amount of times for(initialization;termination;incerement)
Java Loops A nested for loop A nested for loop is a for loop in a for loop
Java Loops for loop can also be missing some of its body Be careful, without the termination, you will get an infinite loop...try it! Press the red box in Eclipse to stop
Java Loops A foreach loop creates a loop that returns for each element of an array or iterable objects from the objects first element to its last
Java Loops A while is a loop that repeats while a condition is true Be careful, without the increment, you will get an infinite loop...try it! Press the red box in Eclipse to stop Can be used on char. Try it, start from k='a' until k<='z' print the value of k
Java Loops A do-while loop is a loop that repeats while the condition is true. However, unlike a while loop, a do-while is checked at the bottom of the loop. Loops always get processed once!! Will not print anything = Will print. What will is print?
Visualizing Data in Java To visualize data, programmers usually use graphs or histograms. In this class we will use the COATJava package developed at the CEBAF Large Acceptance Spectrometer at The Jefferson National Laboratory
Visualizing Data in Java We have already imported the necessary dependancies with our POM file To instantiate a Graph GraphErrors gr = new GraphErrors(); To fill a graph gr.addpoint(x,y,errorx, errory);
Visualizing Data in Java To instantiate a Histogram 1 Dimensional H1F ah1f = new H1F(name, bins, xmin, xmax); 2 Dimensional H2F ah2f = new H2F(name, title, bx, xmin, xmax, by, ymin, ymax); To fill a Histogram ah1f.fill(bin,value); ah2f.fill(binx, biny, value);
Visualizing Data in Java To visualize a graph or histogram After the graph has all values TCanvas canvas = new TCanvas("a Canvas", 800, 800); canvas.draw(gr); To save a graph, use the domain.utils package SaveCanvas.saveCanvas(canvas);
Visualizing Data in Java Solar radius with only gravitational heating 0.0020 Solar radius [r sun] 0.0015 0.0010 0.0005 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 Solar age [t sun]
Homework Make a graph in that shows radius of the sun for 1*ageOfSun-20*ageOfSun (put in units of current solar radius) Submit to Github Extra Credit Try to use Eclipse's content assist to find how to label the x and y axes, as well as give a name to the graph/plot (If you label you should also use units!!)
Next Class More Java Code examples from lecture 2 lecture 3