Created
July 28, 2012 10:16
-
-
Save tedhagos/3192789 to your computer and use it in GitHub Desktop.
RD Programming Concepts Training - Batch 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| @Program name: FindGFC.java | |
| Find the greatest common factor using Euclid's algorithm | |
| */ | |
| class FindGCF { | |
| public static void main(String[] args) { | |
| int smallno = 12; | |
| int bigno = 15; | |
| int rem = 0; | |
| while((rem = bigno % smallno) != 0) { | |
| bigno = smallno; | |
| smallno = rem; | |
| } | |
| if (smallno == 1) { | |
| System.out.println("No GCF was found"); | |
| } | |
| else { | |
| System.out.println("GCF of " + bigno + " and " + smallno + " is " + smallno); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class HelloWorld { | |
| public static void main(String[] args) { | |
| System.out.println("Hello World\n"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| @Program name: KeyboardInput.java | |
| Simple routine to accept input from keyboard | |
| */ | |
| import java.io.IOException; | |
| class KeyboardInput { | |
| final int NUMOFCHARS = 10; // number of characters to accept | |
| String getInput(String prompt) { | |
| String returnValue = ""; | |
| byte b[] = new byte[NUMOFCHARS]; | |
| System.out.print(prompt + " :__________\b\b\b\b\b\b\b\b\b\b"); | |
| try { | |
| System.in.read(b); | |
| } | |
| catch(IOException ioe) { | |
| ioe.printStackTrace(); | |
| } | |
| finally { | |
| returnValue = new String(b); | |
| return returnValue; | |
| } | |
| } | |
| void tryItOut() { | |
| String name = getInput("What is your name? "); | |
| String day = getInput(" what day is it? "); | |
| System.out.print("Hi " + name + " have a nice " + day + " morning\n"); | |
| } | |
| public static void main(String[] args) { | |
| new KeyboardInput().tryItOut(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| @program name: MainParams.java | |
| Just print out all the parameters passed from the command line | |
| */ | |
| class MainParams { | |
| public static void main(String[] args) { | |
| if(args.length == 0) System.exit(0); | |
| int plength = args.length; | |
| for (int i = 0; i < plength ; i++) { | |
| System.out.println(args[i]); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| @Program name: Multiply.java | |
| @Usage: java Multiply <int row> <int col> | |
| Main function throws NumberFormatException in case the user inputs something | |
| that is not an a number. | |
| */ | |
| class Multiply { | |
| public static void main(String args[]) throws NumberFormatException { | |
| if(args.length < 2 ) { | |
| System.out.println("Usage: java Multiply rows cols"); | |
| System.exit(0); | |
| } | |
| int row = Integer.parseInt(args[0]); | |
| int col = Integer.parseInt(args[1]); | |
| for (int irow = 1; irow <= row; irow++) { | |
| for (int jrow = 1; jrow <= row; jrow++) { | |
| System.out.print(irow * jrow + "\t"); | |
| } | |
| System.out.print("\n"); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person { | |
| Person walk() { | |
| System.out.println("walking"); | |
| return this; | |
| } | |
| Person talk() { | |
| System.out.println("talking"); | |
| return this; | |
| } | |
| Person sleep() { | |
| System.out.println("sleeping"); | |
| return this; | |
| } | |
| public static void main(String[] args) { | |
| new Person().walk().talk().sleep(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| First example of inheritance | |
| */ | |
| class Phone { | |
| String number; | |
| Phone() { | |
| } | |
| void call(String numtocall) { | |
| System.out.println("Calling " + numtocall); | |
| } | |
| void answer() { | |
| String color; | |
| System.out.println("Answering"); | |
| } | |
| void setNumber(String args) { | |
| number = args; | |
| System.out.println("Number is " + number); | |
| } | |
| } | |
| class MobilePhone extends Phone { | |
| void sms(String numtarget, String msg) { | |
| System.out.println("sending " + msg + " to " + numtarget); | |
| } | |
| } | |
| class TestPhone { | |
| public static void main(String[] args) { | |
| MobilePhone p = new MobilePhone(); | |
| p.setNumber("12345"); | |
| p.call("678910"); | |
| p.answer(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Shape { | |
| static int count=1; | |
| String color=""; | |
| } | |
| class ShapeTest { | |
| public static void main(String[] args) { | |
| Shape square = new Shape(); | |
| Shape circle = new Shape(); | |
| Shape triangle = new Shape(); | |
| square.color = "white"; | |
| circle.color = "black"; | |
| triangle.color = "red"; | |
| System.out.println("square color " + square.color); | |
| System.out.println("circle color " + circle.color); | |
| System.out.println("triangle color " + triangle.color); | |
| System.out.println("square count " + square.count); | |
| square.count = 10; | |
| System.out.println("square count " + square.count); | |
| System.out.println("circle count " + circle.count); | |
| System.out.println("triangle count " + triangle.count); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class John { | |
| String name = "John"; | |
| } | |
| class Jane { | |
| String name = "Jane"; | |
| } | |
| class Swap { | |
| John a = new John(); | |
| Jane b = new Jane(); | |
| void start() { | |
| swap(a,b); | |
| System.out.println("obj John -> " + a.name); | |
| System.out.println("obj Jane -> " + b.name); | |
| } | |
| void swap(John a, Jane b) { | |
| a.name = "Jane"; | |
| b.name = "John"; | |
| } | |
| public static void main(String[] args) { | |
| new Swap().start(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SwapInt { | |
| int x = 1; | |
| int y = 2; | |
| void start() { | |
| swap(x,y); | |
| System.out.println("x = " + x); | |
| System.out.println("y = " + y); | |
| } | |
| void swap(int a,int b) { | |
| int temp = a; | |
| a = b; | |
| b = temp; | |
| System.out.println("inside the function ------\n"); | |
| System.out.println("x = " + a); | |
| System.out.println("y = " + b); | |
| System.out.println("finished on the function ----\n"); | |
| } | |
| public static void main(String[] args) { | |
| new SwapInt().start(); | |
| } | |
| } |
Author
Author
Why won't the notification fire up on email
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a test comment