class Js_1{
public static void main(String[] args){
int a = 567,b = 675;
System.out.println(a+"と"+b+"の最大公約数は"+gcd(a,b));
}
static int gcd(int a , int b){
int c;
while(a > 1){
c = a;
a = b % a;
b = c;
}
return b;
}
}
|
class Js_1{
public static void main(String[] args){
double a = 567.0,b = 675.0;
System.out.println(a+"と"+b+"の最大公約数は"+gcd(a,b));//間違い
}
static int gcd(int a , int b){
int c;
while(a > 1){
c = a;
a = b % a;
b = c;
}
return b;
}
}
|
System.out.println(a+"と"+b+"の最大公約数は"+gcd((int)a,(int)b));
|
Go to Index
class クラス名{
型 変数名;
型 変数名;
:
コンストラクタ
メソッド
メソッド
:
}
|
import java.io.*;
public class Js_2{
public static void main(String args[]) throws Exception{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("定円の半径=");
double r=(new Double(in.readLine())).doubleValue();
System.out.print("円錐の高さ=");
double h=(new Double(in.readLine())).doubleValue();
Ensui e=new Ensui(r,h);
System.out.println("円錐の体積:"+e.taiseki());
System.out.println("円錐の表面積:"+e.hyoumen());
}
}
class Ensui{
double r;
double h;
Ensui(double r1,double h1){
r=r1;
h=h1;
}
public double taiseki(){
return (r*r*h*Math.PI)/3;
}
public double hyoumen(){
return Math.PI*r*(Math.sqrt(r*r+h*h)+r);
}
}
|
クラス名 オブジェクト名=new クラス名(パラメータ1,パラメータ2,...); |
public class Js_3{
public static void main(String args[]) throws Exception{
Dog poti=new Dog(12.34);
Dog hati=new Dog(6.15);
Dog.number_of_legs=4;
poti.length_of_legs=13.35;
}
}
class Dog{
static int number_of_legs;
double length_of_legs;
Dog(double l){
length_of_legs=l;
}
}
|
インスタンス変数の場合:オブジェクト名.変数名 例:上のプログラムの、poti.length_of_legs; 静的変数の場合 :クラス名.変数名 例:Math.PIなど |
インスタンスメソッドの場合:オブジェクト名.メソッド名 静的メソッドの場合 :クラス名.メソッド名 |
import java.io.*;
public class Js_2{
public static void main(String args[]) throws Exception{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("定円の半径=");
double r=(new Double(in.readLine())).doubleValue();
System.out.print("円錐の高さ=");
double h=(new Double(in.readLine())).doubleValue();
Ensui e=new Ensui(r,h);
System.out.println("円錐の体積:"+e.taiseki());
System.out.println("円錐の表面積:"+e.hyoumen());
}
}
class Ensui{
double r;
double h;
Ensui(double r1,double h1){
r=r1;
h=h1;
}
public double taiseki(){
return (r*r*h*Math.PI)/3;
}
public double hyoumen(){
return Math.PI*r*(Math.sqrt(r*r+h*h)+r);
}
}
|
Go to Index
import java.io.*;
public class Js_6{
public static void main(String args[]) throws Exception{
System.out.println(Oval.menseki(3));
System.out.println(Oval.menseki(3,4));
}
}
public class Oval{
public static double menseki(double a,double b){
return Math.PI*a*b;
}
public static double menseki(double a){
return Math.PI*a*a;
}
}
実行結果:
28.274333882308138
37.69911184307752
|
String str1=new String();
String str2=new String("Hello World");
|
static final double PI=3.14159265358979; |
import java.io.*;
public class Js_7{
public static void main(String args[]){
Person taro=new Person(10,"太郎");
//taro.age=15; ←ageがprotectedだとしてもコンパイルエラー
System.out.println("名前:"+taro.getname());
System.out.println("年齢:"+taro.getage());
Animal poti=new Person(8,"ポチ"); //これは正しい
//System.out.println("名前:"+poti.getname()); ←誤り
System.out.println("名前:"+((Person)poti).getname()); //これなら正しい
System.out.println("年齢:"+poti.getage()); //これも正しい
//Person poti=new Animal(4); ←これも誤り
}
}
class Animal{
private int age;
Animal(int a){
age=a;
}
int getage(){
return age;
}
}
class Person extends Animal{
private String name;
Person(int a,String b){
super(a);
//age=a; ←コンパイルエラー
name=b;
}
String getname(){
return name;
}
}
|
Go to Index
public class Js_4{
public static void main(String args[]) throws Exception{
double a=3.2;
double b=5.8;
double c=a+b;
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("a+b="+c);
}
}
実行結果:
a=3.2
b=5.8
c=9.0
|
public class Js_5{
public static void main(String args[]) throws Exception{
String a=new String("Hello");
String b=new String("World");
System.out.println(a+b);
}
}
実行結果:
HelloWorld
|
import java.io.*;
public class Js_7{
public static void main(String args[]){
Person taro=new Person(10,"太郎");
System.out.println("名前:"+taro.getname());
System.out.println("年齢:"+taro.getage());
Animal poti=new Person(8,"ポチ"); //これは正しい
//System.out.println("名前:"+poti.getname()); ←誤り
System.out.println("名前:"+((Person)poti).getname()); //これなら正しい
System.out.println("年齢:"+poti.getage()); //これも正しい
//Person poti=new Animal(4); ←これも誤り
}
}
class Animal{
private int age;
Animal(int a){
age=a;
}
int getage(){
return age;
}
}
class Person extends Animal{
private String name;
Person(int a,String b){
super(a);
name=b;
}
String getname(){
return name;
}
}
|
Animal poti=new Person(8,"ポチ"); |
Go to Index