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;

}
}