本文共 12864 字,大约阅读时间需要 42 分钟。
public class book { public static void main(String[] args) { Rectangle r1 = new Rectangle(4,40); Rectangle r2 = new Rectangle(3.5,35.9); System.out.println("R1 "+r1.getWidth()+" "+r1.getHeight()+" "+r1.getArea()+" "+r1.getPerimeter()); System.out.println("R2 "+r2.getWidth()+" "+r2.getHeight()+" "+r2.getArea()+" "+r2.getPerimeter()); }}class Rectangle{ private double width; private double height; public Rectangle(double w, double h) { this.width=w; this.height=h; } public Rectangle() { this(1,1); } public double getArea() { return width*height; } public double getPerimeter() { return 2*(width+height); } public double getWidth() { return width; } public double getHeight() { return height; }}
public class book { public static void main(String[] args) { Stock s = new Stock("ORCL","Oracle Corporation"); s.setPreviousClosingPrice(34.5); s.setCurrentPrice(34.35); System.out.println("The change percent is "+s.getChangePercent()+"%"); }}class Stock{ private String symbol; private String name; private double previousClosingPrice; private double currentPrice; Stock(String symbol,String name) { this.symbol=symbol; this.name=name; } public void setPreviousClosingPrice(double previousClosingPrice) { this.previousClosingPrice = previousClosingPrice; } public void setCurrentPrice(double p) { this.currentPrice=p; } public double getChangePercent() { return (currentPrice-previousClosingPrice)/previousClosingPrice*100; }}
import java.util.Date;public class book { public static void main(String[] args) { long second = 10000; for(int i=0;i<8;i++) { Date curr = new Date(second); System.out.println(curr.toString()); second*=10; } }}
import java.util.Random;public class book { public static void main(String[] args) { Random r = new Random(1000); for(int i=0;i<50;i++) System.out.print(r.nextInt(100)+" "); }}
import java.util.GregorianCalendar;public class book { public static void main(String[] args) { GregorianCalendar g = new GregorianCalendar(); System.out.println(g.get(GregorianCalendar.YEAR)+"/"+g.get(GregorianCalendar.MONTH)+"/"+g.get(GregorianCalendar.DAY_OF_MONTH)); g.setTimeInMillis(123456789876L); System.out.println(g.get(GregorianCalendar.YEAR)+"/"+g.get(GregorianCalendar.MONTH)+"/"+g.get(GregorianCalendar.DAY_OF_MONTH)); }}
public class book { public static void main(String[] args) { StopWatch timer = new StopWatch(); //selection sort timer.stop(); System.out.println(timer.getElapsedTime()); }}class StopWatch{ private long startTime; private long endTime; StopWatch() { this.startTime=System.currentTimeMillis(); } public void start() { this.startTime=System.currentTimeMillis(); } public void stop() { this.endTime=System.currentTimeMillis(); } public long getElapsedTime() { return this.endTime-this.startTime; }}
import java.util.Date;public class book { public static void main(String[] args) { Account swy = new Account(1122,2000); swy.setAnnualInterestRate(4.5/100); swy.withDraw(2500); swy.deposit(3000); System.out.println("Balance: "+swy.getBalance()); System.out.println("Monthly Interest: "+swy.getMonthlyInterest()); System.out.println("Register Date: "+swy.getDateCreated().toString()); }}class Account{ private int id; private double balance; private double annualInterestRate; private Date dateCreated; public Account() { id=0; balance=0; annualInterestRate=0; dateCreated=new Date(); } public Account(int di,double b) { id=di; balance=b; annualInterestRate=0; dateCreated=new Date(); } public int getId(){ return id; } public void setId(int j){ id=j; } public double getBalance(){ return balance; } public void setBalance(double j){ balance=j; } public double getAnnualInterestRate(){ return annualInterestRate; } public void setAnnualInterestRate(double j){ annualInterestRate=j; } public Date getDateCreated(){ return dateCreated; } public double getMonthlyInterestRate() { return annualInterestRate/12; } public double getMonthlyInterest(){ return annualInterestRate/12*balance; } public void withDraw(double m){ balance-=m; } public void deposit(double m){ balance+=m; }}
public class book { public static void main(String[] args) { Fan fan1 = new Fan(); fan1.setSpeed(Fan.FAST); fan1.setRadius(10); fan1.setColor("yellow"); fan1.setOn(true); Fan fan2 = new Fan(); fan2.setSpeed(Fan.MEDIUM); System.out.println(fan1.toString()); System.out.println(fan2.toString()); }}class Fan{ final static int SLOW=1; final static int MEDIUM=2; final static int FAST=3; private int speed = SLOW; private boolean on = false; private double radius = 5; private String color="blue"; public int getSpeed(){ return speed; } public void setSpeed(int s){ speed=s; } public boolean getOn(){ return on; } public void setOn(boolean j){ on=j; } public double getRadius(){ return radius; } public void setRadius(double r){ radius=r; } public String getColor(){ return color; } public void setColor(String s){ color=s; } public Fan(){ speed=SLOW; on=false; radius=5; color="blue"; } public String toString(){ String re=""; if(this.on) re=re+speed+" "+color+" "+radius; else re=re+"fan is off "+color+" "+radius; return re; }}
public class book { public static void main(String[] args) { RegularPolygon p = new RegularPolygon(); RegularPolygon pp = new RegularPolygon(6,4); RegularPolygon ppp = new RegularPolygon(10,4,5.6,7.8); System.out.println(p.getPerimeter()+" "+p.getArea()); System.out.println(pp.getPerimeter()+" "+pp.getArea()); System.out.println(ppp.getPerimeter()+" "+ppp.getArea()); }}class RegularPolygon{ private int n=3; private double side=1; private double x=0; private double y=0; public RegularPolygon(){ n=3; side=1; } public RegularPolygon(int num,double len){ n=num; side=len; } public RegularPolygon(int hum,double len,double xx,double yy){ n=hum; side=len; x=xx; y=yy; } public void setN(int nn){ n=nn; } public int getN(){ return n; } public void setSide(double ss){ side=ss; } public double getSide(){ return side; } public void setX(double xx){ x=xx; } public double getX(){ return x; } public void setY(double yy){ y=yy; } public double getY(){ return y; } public double getPerimeter(){ return n*side; } public double getArea(){ return n*side*side/(4*Math.tan(Math.PI/n)); }}
import java.util.Scanner;public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a b c: "); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); QuadraticEquation ass = new QuadraticEquation(a,b,c); if(ass.getDiscriminant()<0) System.out.println("The equation has no roots."); else if(ass.getDiscriminant()==0) System.out.println("One root "+ass.getRoot1()); else System.out.println("Two roots "+ass.getRoot1()+" "+ass.getRoot2()); }}class QuadraticEquation{ private double a; private double b; private double c; public QuadraticEquation(double aa,double bb,double cc){ a=aa; b=bb; c=cc; } public double getA(){ return a; } public double getB(){ return b; } public double getC(){ return c; } public double getDiscriminant(){ return b*b-4*a*c; } public double getRoot1(){ return (-1*b+Math.sqrt(b*b-4*a*c))/(2*a); } public double getRoot2(){ return (-1*b-Math.sqrt(b*b-4*a*c))/(2*a); }}
import java.util.Scanner;public class book { public static void main(String[] args) { System.out.print("Enter a b c d e f: "); Scanner input = new Scanner(System.in); double a = input.nextDouble(); double b = input.nextDouble(); double c = input.nextDouble(); double d = input.nextDouble(); double e = input.nextDouble(); double f = input.nextDouble(); LinearEquation ass = new LinearEquation(a,b,c,d,e,f); if(ass.isSolvable()) System.out.println(ass.getX()+" "+ass.getY()); else System.out.println("The equation has no solutions."); }}class LinearEquation{ private double a,b,c,d,e,f; public LinearEquation(double aa,double bb,double cc,double dd,double ee,double ff){ a=aa; b=bb; c=cc; d=dd; e=ee; f=ff; } public double getA(){ return a; } public double getB(){ return b; } public double getC(){ return c; } public double getD(){ return d; } public double getE(){ return e; } public double getF(){ return f; } public boolean isSolvable(){ return a*d-b*c!=0; } public double getX(){ return (e*d-b*f)/(a*d-b*c); } public double getY(){ return (a*f-e*c)/(a*d-b*c); }}
import java.util.Scanner;public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the four points x1 y1 x2 y2 x3 y3 x4 y4: "); double x1=input.nextDouble(); double y1=input.nextDouble(); double x2=input.nextDouble(); double y2=input.nextDouble(); double x3=input.nextDouble(); double y3=input.nextDouble(); double x4=input.nextDouble(); double y4=input.nextDouble(); LinearEquation ass = new LinearEquation(y1-y2,x2-x1,y3-y4,x4-x3,(y1-y2)*x1-(x1-x2)*y1,(y3-y4)*x3-(x3-x4)*y3); if(ass.isSolvable()) System.out.println(ass.getX()+" "+ass.getY()); else System.out.println("No joining."); }}class LinearEquation{ private double a,b,c,d,e,f; public LinearEquation(double aa,double bb,double cc,double dd,double ee,double ff){ a=aa; b=bb; c=cc; d=dd; e=ee; f=ff; } public double getA(){ return a; } public double getB(){ return b; } public double getC(){ return c; } public double getD(){ return d; } public double getE(){ return e; } public double getF(){ return f; } public boolean isSolvable(){ return a*d-b*c!=0; } public double getX(){ return (e*d-b*f)/(a*d-b*c); } public double getY(){ return (a*f-e*c)/(a*d-b*c); }}
import java.util.Scanner;public class book { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the number of rows and columns in the array: "); int rows = input.nextInt(); int columns = input.nextInt(); double[][] dick = new double[rows][columns]; System.out.print("Enter the array: "); for(int i=0;iass.maxValue) { ass.row=i; ass.maxValue=a[i][j]; ass.column=j; } } } return ass; }}class Location{ public int row; public int column; public double maxValue;}
转载地址:http://suwai.baihongyu.com/