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


}


}

TestComplexNumber


public class TestComplexNumber {

public static void main(String[] args) {

//I decide two numbers

ComplexNumber data1=new ComplexNumber(1,1);

ComplexNumber data2=new ComplexNumber(1,1);

System.out.println("Number1+Number2="+data1.add(data2)); //data1+data2 operation

System.out.println("Number1-Number2="+data1.subtract(data2)); //data1-data2 operation

System.out.println("Number1*Number2="+data1.multiply(data2)); //data1*data2 operation

System.out.println("Number1/Number2="+data1.divide(data2)); //data1/data2 operation

System.out.println("Conjugate Number1 ="+data1.conjugate(data1)+" Conjugate Number1 ="+data2.conjugate(data2)); //if we take like this there is no problem and the followings

System.out.println("Reciprocal Number1 ="+data1.reciprocal()+" Reciprocal Number2 ="+data2.reciprocal());

System.out.println("Number1's angle="+data1.getAngle(data1)+" Number2's angle="+data2.getAngle(data2));

System.out.println("Number1's magnitude="+data1.getMagnitude(data1)+" Number2's magnitude="+data2.getMagnitude(data2));

if(data1.equals(data2)){
System.out.println("The two numbers are equal");
}else{
System.out.println("The two numbers are not equal");
}

}

}

ComplexNumber(Karmaşık Sayılar)


public class ComplexNumber
{
//Imaginary and real numbers

private double im,re;

//Constructor to decide numbers (r,ri)

public ComplexNumber(double r,double i)
{
this.re=r;
this.im=i;
}

public double getReal()
{
return this.re;
}

public double getImaginary()
{
return this.im;
}

//this provides to take reciprocal

public ComplexNumber reciprocal()
{
double bölüm=re*re+im*im;
return new ComplexNumber(re/bölüm,-im/bölüm);
}

//add operation

public ComplexNumber add(ComplexNumber newnumber)
{
double x=re+newnumber.re;
double y=im+newnumber.im;

return new ComplexNumber(x,y);
}

//subtract operation

public ComplexNumber subtract(ComplexNumber newnumber)
{
double sub=re-newnumber.re;
double sub2=im-newnumber.im;
return new ComplexNumber(sub,sub2);
}

//multiply operation

public ComplexNumber multiply(ComplexNumber newnumber)
{
double op1=im*newnumber.re+re*newnumber.im;
double mul2=im*newnumber.im*-1;
double mul=newnumber.re*re+mul2;
return new ComplexNumber(mul,op1);
}

//divide operation

public ComplexNumber divide(ComplexNumber newnumber)
{
double ac=(im*newnumber.re-newnumber.im*(re));
double av=im*newnumber.re+re*newnumber.im;
double ab=re*newnumber.re+im*newnumber.im;
    return new ComplexNumber(av/ab,ac/ab);

}

//Conjugation operation

public ComplexNumber conjugate(ComplexNumber newnumber)
{
return new ComplexNumber(re,-im);
}

//equals:if re and im equal so they are equal

public boolean equals(ComplexNumber newnumber)
{
return (re==newnumber.re && im==newnumber.im);
}

//Get angle:I benefit from atan and todegrees from Math class of java.

public double getAngle(ComplexNumber newnumber)
{
return Math.toDegrees(Math.atan(re/im));
}

//Magnitude:squart (re)^2+(im)^2

public double getMagnitude(ComplexNumber newnumber)
{
return Math.sqrt(Math.pow(re,2)+Math.pow(im, 2));
}

public String toString()
{
if(im==0){ //im=0 so we dont write "i"
return re+"";
}
if(im<0){
return re+"-"+ (-im)+"i";   //if im<0 i write -im and symbol is not +,it is-,
}
if(re==0){
return im+"i"; //if re =0 we dont write re
}
return re+"+"+im+"i";
}

}






CelsiustoFahrenheitconversion(Celcius 'u FFahrenheit 'a çevirme programı


public class CelsiustoFahrenheitconversion {

public static void main(String[] args) {

//Created for loop to write all temperatures

for(double i =0; i<=20; i++){

//Every value counted and calculated

System.out.println("Temperature="+ i +" Celcius "+ (((9.0/5)*i)+32)+ " Fahrenheit");

}

}

}

Area Calculator(alan hesaplayıcı)

import java.util.Scanner;

public class AreaCalculator {

public static void main(String[] args) {
//Taking inputs from the user
Scanner input=new Scanner(System.in);

System.out.println("Enter the number of sides");

int n = input.nextInt();

System.out.println("Enter the side");

double side = input.nextDouble();

calculateArea(n,side);


}
public static double calculateArea(int n, double side){

//Calculation for the finding area
double area = (n*side*side)/(4*(Math.tan(Math.PI/n)));

System.out.println("area is "+ area);

return area;


}
}

Arraylerde kullanıcıdan girdi alarak girdileri toplamak

import java.util.*;
public class ArraydeToplama{
public static void main(String args[]){
int[]myList=new int[5];
int toplam=0;
Scanner input=new Scanner(System.in);

//Kullanıcıdan 5 sayı almasını sağladım.
for(int i=0;i<5;i++){
System.out.println("Sayıları giriniz");
myList[i]=input.nextInt();
}

//Burada da gelen sayıları toplamaya yarayan bi loop var.
for(int x=0;x<mylist.length;x++){
toplam+=myList[x];
}
System.out.println("Sonuç="+toplam);
}
}