2 Ocak 2015 Cuma

DeckOfCards


public class DeckOfCards {
public static void main(String args[]){
int [] deck=new int[52];

String []suits={"Spades","Hearts","Diamonds","Clubs"};
String []ranks={"Ace","1","2","3","4","5","6","7","8","9","10","Jack","Queen","King"};


for(int i=0;i<deck.length;i++){
deck[i]=i;
}

for(int i=0;i<deck.length;i++){

int index=(int)(Math.random()*deck.length);
int temp=deck[i];
deck[i]=deck[index];
deck[index]=temp;
}

for(int i=0;i<4;i++){
String suit=suits[deck[i]/13];
String rank=ranks[deck[i]%13];
System.out.println("Card number "+deck[i]+":"+rank+" of "+suit);

}







}
}

PalindromeInteger

import java.util.*;

public class PalindromeInteger {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);
 
System.out.println("Please write an integer value");
int num=input.nextInt();
 
   System.out.println(isPalindrome(num));
   System.out.println("Reverse number is= "+reverse(num));
}
public static int reverse(int number){

int x=number%10;
int y=number/10;
int z=y%10;
int c=y/10;
int v=c%10;

int newNumber=x*100+z*10+c;
return newNumber;
}
public static boolean isPalindrome(int number)
{
int newNumber=reverse(number);

if(number==newNumber){
System.out.println("Number is Palindrome");
}else
System.out.println("It is not Palindrome number");

return true ;

}
}

1 Ocak 2015 Perşembe

GreatestCommonDivisor

import java.util.*;

public class GreatestCommonDivisor{
public static void main(String args[]){

Scanner input=new Scanner(System.in);

System.out.println("Enter number1");
int num1=input.nextInt();
System.out.println("Enter number2");
int num2=input.nextInt();
if(num1==0 || num2==0){
System.out.println("Greatest Common divisor is 0");
}else
System.out.println("Greatest Common divisor is "+gcd(num1,num2));

}
public static int gcd(int x,int y){
int gcd=1;
int m=2;

while(m<=x && m<=y){
if(x%m==0 && y%m==0){
gcd=m;
}
m++;
}
return gcd;
}
}

Having return in methods

          If a statement has return type we can write it:(for example System.out.println("Grade is"+getGrade(grade));
          But if it does not have return type we write:(for example
System.out.print("Grade is ");
getGrade(grade);

WhatIsMyGrade

public class WhatIsMyGrade{
public static void myGrade(double grade){

if(grade>=90){
System.out.println("Grade is A");
}else if(grade>=80){
System.out.println("Grade is B");
}else if(grade>=70){
System.out.println("Grade is C");
}else if(grade>=60){
System.out.println("Grade is D");
}else
System.out.println("Grade is F");
}

public static void main(String args[]){
myGrade(14);
}
}

Call Stack

           Each time a method is invoked,the system creates an activation records that stores parameter and variables for the method and places the activation record in an area of memory known as a call stack.A call stack is also known as an execution stack,runtime,stack or machine stack and iti is often shortened to just -the stack-.When a method calls another method,the caller's activation record is kept intact,and a new activation record is created for the new method called.When a method finishes its work and returns to its caller,its activation record is removed from the call stack.
            A call stack stores the activation records in a last-in,first-out fashion:The activation record for the method that is invoked last is removed first from the stack.

MaxMethod

public class MaxMethod{
public static double max(double num1,double num2){

double result;
if(num1>num2){
result=num1;
}else if(num1==num2){
result=num1;
}else
result=num2;

return result;
}
public static void main(String args[]){
System.out.println("Max value is "+max(13,15));

}

}