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));

}

}

SumWithMethod

public class SumWithMethod{

public static int sum(int i1,int j1){

int sum=0;
for(int i=i1; i<j1; i++){
sum+=i;
}
return sum;
}
public static void main(String args[]){
System.out.println("Sum is= "+sum(5,7));
}
}

RepeatAddition(Guess Game)

import java.util.*;

public class RepeatAddition{
public static void main(String args[]){
int i=(int)(Math.random()*100);
int j=(int)(Math.random()*100);

Scanner input=new Scanner(System.in);

System.out.println("What is " + i +"+"+ j +"?");

int answer=input.nextInt();

while(i + j != answer){
System.out.println("Wrong answer try again.What is " + i+ "+" + j + "?");
answer=input.nextInt();
}
System.out.println("Correct answer");


}

}

ToSumSeriesNumber

public class ToSumSeriesNumber{
public static void main(String args[]){
int i=0;
int sum=0;
while(i<5){
sum=sum+i;
i++;
}
System.out.println("Sum of 1 2 3 4 ="+sum);
}


}

IntroductionToLoop(Typing Welcome to Java 100 times)

public class IntroductionToLoop{
public static void main(String args[]){
for(int i=0;i<100;i++){
System.out.println("Welcome to Java");
}

//Other types to write Welcome to Java
System.out.println("After that while loop is working");
int i=0;
while(i<100){
System.out.println("Welcome to Java");
i++;

}
}
}

Algebra(solve quadratic equations)

import java.util.*;

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

Scanner input=new Scanner(System.in);
System.out.println("Please write 3 numbers to show ax^2+bx+c equation");

System.out.print("a=");
double a=input.nextDouble();
System.out.print("b=");
double b=input.nextDouble();
System.out.print("c=");
double c=input.nextDouble();

double discriminant=(b*b)-(4*a*c);

double r1=(-b+Math.pow(discriminant,0.5))/(2*a);

double r2=(-b-Math.pow(discriminant,0.5))/(2*a);

double r3=(-b/(2*a));

if(discriminant>0){
System.out.println("Equation has two roots,"+"\nr1="+r1+"\nr2="+r2);
}
if(discriminant==0){
System.out.println("Equation has one root,"+"\nr="+r3);
}
if(discriminant<0){
System.out.println("There is no root");
}
}
}

29 Aralık 2014 Pazartesi

TimeConvertor (Zaman Çevirici)

import java.util.Scanner;

public class TimeConvertor {

public static void main(String[] args) {

System.out.println("Please write milliseconds ");

//Input for milliseconds

Scanner input = new Scanner(System.in);

//Operations for finding hours minutes seconds

long millis = input.nextLong();


//Method's name

convertMillis(millis);

}

public static void convertMillis(long millis){

//Calculation for the time

long hours = millis / 3600000;

long remaininghours = millis % 3600000;

long minutes = remaininghours / 60000;

long remainingminutes = remaininghours % 60000;

long seconds = remainingminutes / 1000;

System.out.println(hours + ":" + minutes +":"+ seconds);



}


}

TestRectangle


public class TestRectangle {

public static void main(String[] args) {

//Datas are created
Rectangle data1=new Rectangle(4,40);

Rectangle data2=new Rectangle(3.5,35.9);

//This calls to string method and implements data1 and data2
System.out.println(""+data1+data2);

//This area is not necessary because I created a new storage space.Methods implement datas.
double area=data1.getArea();
double area2=data2.getArea();
System.out.println(" First area is "+data1.getArea()+".Second area is "+data2.getArea());

//This perimeter is not necessary because I created a new storage space.Methods implement datas.
double perimeter=data1.getPerimeter();
double perimeter2=data2.getPerimeter();
System.out.println(" First perimeter is "+data1.getPerimeter()+" .Second perimeter is "+data2.getPerimeter());

}

}

Rectangle


public class Rectangle {

public double width,height;

//Representing of default values
public Rectangle()
{
width=1;
height=1;
}
//A new constructor for take datas in one line
public Rectangle(double width,double height){
this.width=width;
this.height=height;

}
//This is for taking width
public double getWidth(){
return width;
}
//This is for taking height
public double getHeight(){
return height;
}
//This provides to calculate area
public double getArea(){
return width*height;
}
//This provides to calculate perimeter
public double getPerimeter(){
return 2*(width+height);
}
//Output is shown to be string
public String toString(){
return " Rectangle.width="+this.width+" Rectangle.height="+this.height;
}

}

PerfectNumbers(Mükemmel Sayılar)

public class PerfectNumbers{
public static void main(String[]args){
//Specifying the intervals of numbers
for(int i=1; i<10000; i++){

int sum = 0;

for(int divisors=1; i/2>=divisors; divisors++){
//Finding the remainding
int result = i%divisors;

if(result==0){
sum+=divisors;
}

}

if(sum==i){
System.out.println(sum);
}



}
}
}

OccurenceofMaxNumbers(Maksimum sayı ve onun miktarı)

import java.util.Scanner;
//The class name
public class OccurenceofMaxNumbers{

public static void main(String[]args){
//Taking the datas from users
Scanner input =new Scanner(System.in);
//Specifying values intervals
int sayı=0,max,count=0;
System.out.println("sayı gir");
//Taking the datas
sayı=input.nextInt();

//If they enter 0 the program will finish
max=sayı;
while(sayı!=0){
if (sayı>max){
max=sayı;
}else if(sayı==max)
//This counts max values
count++;

System.out.println("sayı gir");
sayı=input.nextInt();
}
System.out.println("The largest number is "+ max+" and the occurrence count of the largest number is "+ count);
}
}

IsPointInTheRectangle(Verilen nokta üçgenin içinde mi değil mi?)

import javax.swing.JOptionPane;

public class IsPointInTheRectangle {

public static void main(String[] args){
//Enter coordinate x
double x;
x =Double.parseDouble(JOptionPane.showInputDialog(null,"Please write the coordinate x"));
//Enter coordinate y
double y;
y = Double.parseDouble(JOptionPane.showInputDialog(null,"Please write the coordinate y"));

//Enter the conditions
if(x<5 && x>=0 && y<10 && y>=0)
{
JOptionPane.showMessageDialog(null,"Your values are in the rectangle"  );
}
else
JOptionPane.showMessageDialog(null,"Your values are not in the rectangle");

}
}

DecimalToBinary (10'luk tabandaki sayıyı 2'lik tabana çevirme)

import java.util.Scanner;

public class DecimalToBinary {
public static void main(String [] args){
//Taking input from user

Scanner input = new Scanner(System.in);

System.out.print("Enter a positive integer:");

int x=input.nextInt();

//Taking be a string to integer and goes to method

System.out.println(""+toBinary(x));

}
public static String toBinary(int n){

//This program calculates just positive integers
if(n<=-1){
System.out.println("Please write positive integer");
return null;
}
//Necessary calculation to convert decimal to binary
String binary ="";
while(n>0){
int remaining = n%2;
binary=remaining+binary;
n/=2;

}
binary = n+binary;

return binary;

}
}

ComputeThePerimeterOfaTriangle(Üçgenin çevresini hesaplama)

import javax.swing.JOptionPane;

public class ComputeThePerimeterOfaTriangle {
public static void main(String args[]){
//Taking the edges of triangle
double edge1;
edge1= Double.parseDouble(JOptionPane.showInputDialog(null,"Enter the first edge!"));
double edge2;
edge2 = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter the second edge!"));
double edge3;
edge3 = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter the third edge!"));
//Find the environment of triangle
double perimeter = edge1 + edge2 + edge3;

//This part means what is qualification to be a triangle
if((edge1+edge2) > edge3 && (edge3+edge2)>edge1 && (edge1+edge3)>edge2 && edge1!=0 && edge2!=0 && edge3!=0 )
{
JOptionPane.showMessageDialog(null,"Perimeter of triangle is " + perimeter);
}
else
JOptionPane.showMessageDialog(null,"The triangle is not created!");


}


}