import java.util.Date;
import java.util.Scanner;
public class book {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter three sides, color and filled: ");
double s1=input.nextDouble();
double s2=input.nextDouble();
double s3=input.nextDouble();
String c=input.next();
boolean filled = input.nextBoolean();
Triangle ass = new Triangle(s1,s2,s3);
ass.setColor(c);
ass.setFilled(filled);
System.out.println("area: "+ass.getArea()+" perimeter: "+ass.getPerimeter()+
"\ncolor: "+ass.getColor()+" filled: "+ass.isFilled());
}
}
class GeometricObject{
private String color = "white";
private boolean filled;
private Date dateCreated;
public GeometricObject(){
dateCreated=new Date();
}
public GeometricObject(String color,boolean filled){
dateCreated=new Date();
this.color=color;
this.filled=filled;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color=color;
}
public boolean isFilled(){
return filled;
}
public void setFilled(boolean filled){
this.filled=filled;
}
public Date getDateCreated(){
return dateCreated;
}
public String toString(){
return "created on "+dateCreated+"\ncolor: "+color+
" and filled: "+filled;
}
}
class Triangle extends GeometricObject{
private double side1;
private double side2;
private double side3;
public Triangle(){
super();
side1=side2=side3=1;
}
public Triangle(double s1,double s2,double s3){
super();
side1=s1;
side2=s2;
side3=s3;
}
public double getSide1(){
return side1;
}
public double getSide2(){
return side2;
}
public double getSide3(){
return side3;
}
public double getArea(){
double p=(side1+side2+side3)/2;
return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
}
public double getPerimeter(){
return side1+side2+side3;
}
public String toString(){
return "Triangle: side1 = "+side1+" side2 = "+side2+" side3 = "+side3;
}
}
略
import java.util.Date;
public class book {
public static void main(String[] args)
{
Account swy = new Account(1122,2000);
CheckingAccount swySwy = new CheckingAccount(114,514,1919);
SavingAccount swySwySwy = new SavingAccount();
System.out.println(swy.toString()+" "+swySwy.toString()+" "+swySwySwy.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;
}
}
class CheckingAccount extends Account{
private double overDraw;
public CheckingAccount()
{
super();
}
public CheckingAccount(int di,double bb,double oo){
super(di,bb);
overDraw=oo;
}
}
class SavingAccount extends Account{
}
import java.util.Scanner;
import java.util.ArrayList;
public class book {
public static void main(String[] args)
{
System.out.print("Enter numbers finished with 0: ");
Scanner input = new Scanner(System.in);
ArrayList<Integer> ass = new ArrayList<Integer>();
int tmp=input.nextInt();
while(tmp!=0){
ass.add(tmp);
tmp=input.nextInt();
}
System.out.println("The maximum is "+max(ass));
}
public static Integer max(ArrayList<Integer> list){
if(list==null)
return null;
else if(list.size()==0)
return null;
else
{
return java.util.Collections.max(list);
}
}
}
略
import java.util.ArrayList;
import java.util.Date;
public class book {
public static void main(String[] args)
{
ArrayList<Object> ass = new ArrayList<>();
String animal = "114514";
Date dday = new Date();
ass.add(animal);
ass.add(dday);
for (Object o : ass) {
System.out.println(o.toString());
}
}
}
public static void shuffle(ArrayList<Integer> list){
java.util.Collections.shuffle(list);
}
import java.util.ArrayList;
import java.util.Date;
public class book {
public static void main(String[] args)
{
Account ass = new Account(1122,1000,"George");
ass.deposit(30,"1st dep");
ass.deposit(40,"2nd dep");
ass.deposit(50,"3rd dep");
ass.withDraw(5,"1st wdw");
ass.withDraw(4,"2nd wdw");
ass.withDraw(2,"3rd wdw");
System.out.print("name :"+ass.getName()+" rate: "+ass.getAnnualInterestRate()+" balance: "+ass.getBalance()+"\n");
for(Transaction dick:ass.transactions) {
System.out.println("date: "+dick.date);
System.out.println("type: " + dick.type);
System.out.println("amount: "+dick.amount);
System.out.println("new balance: "+dick.balance);
System.out.println("description: "+dick.description);
}
}
}
class Account
{
private int id;
private double balance;
private double annualInterestRate;
private Date dateCreated;
private String name;
ArrayList<Transaction> transactions;
public Account()
{
id=0;
balance=0;
annualInterestRate=0;
dateCreated=new Date();
transactions=new ArrayList<Transaction>();
}
public Account(int di,double b)
{
id=di;
balance=b;
annualInterestRate=0;
dateCreated=new Date();
transactions=new ArrayList<Transaction>();
}
public Account(int di,double b,String n)
{
id=di;
balance=b;
annualInterestRate=0;
dateCreated=new Date();
name=n;
transactions=new ArrayList<Transaction>();
}
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,String ass){
balance-=m;
transactions.add(new Transaction('W',m,balance,ass));
}
public void deposit(double m,String ass){
balance+=m;
transactions.add(new Transaction('D',m,balance,ass));
}
public void setName(String n){
name=n;
}
public String getName(){
return name;
}
}
class Transaction{
public Date date;
public char type;
public double amount;
public double balance;
public String description;
public Transaction(char t,double am,double bl,String des){
date=new Date();
type=t;
amount=am;
balance=bl;
description=des;
}
}
import java.util.Scanner;
import java.util.ArrayList;
public class book {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the array size n: ");
int ass = input.nextInt();
int[][] dick = new int[ass][ass];
ArrayList<Integer> rows = new ArrayList<>();
ArrayList<Integer> columns = new ArrayList<>();
for(int i=0;i<ass;i++)
{
for(int j=0;j<ass;j++)
dick[i][j]=(int)(Math.random()*2);
}
for(int i=0;i<ass;i++)
{
int sum=0;
for(int j=0;j<ass;j++){
if(dick[i][j]==1)
sum++;
}
rows.add(sum);
}
for(int j=0;j<ass;j++)
{
int sum=0;
for(int i=0;i<ass;i++){
if(dick[i][j]==1)
sum++;
}
columns.add(sum);
}
System.out.println("The random array is ");
for(int i=0;i<ass;i++){
for(int j=0;j<ass;j++){
System.out.print(dick[i][j]+" ");
}
System.out.print('\n');
}
System.out.println("The largest row index: "+java.util.Collections.max(rows));
System.out.println("The largest column index: "+java.util.Collections.max(columns));
}
}
import java.util.Scanner;
import java.util.ArrayList;
public class book {
public static void main(String[] args)
{
System.out.print("Enter 5 strings: ");
Scanner input = new Scanner(System.in);
MyStack ass = new MyStack();
for(int i=0;i<5;i++)
{
String tmp = input.next();
ass.push(tmp);
}
while(!ass.isEmpty())
System.out.println(ass.pop());
}
}
class MyStack extends ArrayList
{
public void push(Object o){
super.add(o);
}
public Object pop(){
Object ass = super.get(size()-1);
super.remove(size()-1);
return ass;
}
public boolean isEmpty(){
return super.isEmpty();
}
}
import java.util.Scanner;
import java.util.ArrayList;
public class book {
public static void main(String[] args)
{
System.out.print("Enter 5 integers: ");
Scanner input = new Scanner(System.in);
ArrayList<Integer> jesus = new ArrayList<>();
for(int i=0;i<5;i++){
int tmp = input.nextInt();
jesus.add(tmp);
}
sort(jesus);
for(int i=0;i<5;i++)
System.out.print(jesus.get(i)+" ");
}
public static void sort(ArrayList<Integer> list){
java.util.Collections.sort(list);
}
}
import java.util.Scanner;
import java.util.ArrayList;
public class book {
public static void main(String[] args)
{
System.out.print("Enter 5 doubles: ");
Scanner input = new Scanner(System.in);
ArrayList<Double> jesus = new ArrayList<>();
for(int i=0;i<5;i++){
double tmp = input.nextDouble();
jesus.add(tmp);
}
System.out.println("The sum is "+sum(jesus));
}
public static double sum(ArrayList<Double> ass){
double ss = 0;
for (Double aDouble : ass) ss += aDouble;
return ss;
}
}
import java.util.Scanner;
import java.util.ArrayList;
public class book {
public static void main(String[] args)
{
System.out.print("Enter 10 integers: ");
Scanner input = new Scanner(System.in);
ArrayList<Integer> jesus = new ArrayList<>();
for(int i=0;i<10;i++){
int tmp = input.nextInt();
jesus.add(tmp);
}
removeDuplicate(jesus);
for (Integer integer : jesus) System.out.print(integer + " ");
}
public static void removeDuplicate(ArrayList<Integer> list){
int ptr=0;
while(ptr<list.size()){
int tmp = list.get(ptr);
if(list.indexOf(tmp)!=ptr)
list.remove(ptr);
else
ptr++;
}
}
}
import java.util.Scanner;
import java.util.ArrayList;
public class book {
public static void main(String[] args)
{
System.out.print("Enter 5 integers for list1: ");
Scanner input = new Scanner(System.in);
ArrayList<Integer> jostar = new ArrayList<>();
for(int i=0;i<5;i++){
int tmp = input.nextInt();
jostar.add(tmp);
}
System.out.print("Enter 5 integers for list2: ");
ArrayList<Integer> joseph = new ArrayList<>();
for(int i=0;i<5;i++){
int tmp = input.nextInt();
joseph.add(tmp);
}
ArrayList<Integer> dio = union(jostar,joseph);
for(int j:dio)
System.out.print(j+" ");
}
public static ArrayList<Integer> union(
ArrayList<Integer> list1,ArrayList<Integer> list2
){
ArrayList<Integer> josuke = new ArrayList<>();
josuke.addAll(list1);
josuke.addAll(list2);
return josuke;
}
}
矩阵计算,略
import java.util.Scanner;
import java.util.ArrayList;
public class book {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer m: ");
int m = input.nextInt();
int oldm=m;
ArrayList<Integer> divides = new ArrayList<>();
while(m!=1)
{
for(int i=2;i<=m;i++)
{
if(m%i==0){
divides.add(i);
m/=i;
break;
}
}
}
int n=1;
int sb = 1;
for(int jj:divides){
if(occur(divides,jj)%2==1&&jj!=sb){
n*=jj;
sb=jj;
}
}
System.out.println(n);
System.out.println(oldm*n);
}
public static int occur(ArrayList<Integer> l,int k){
int count=0;
for(int j:l) {
if (j == k)
count++;
}
return count;
}
}
public static ArrayList<Character> toCharacterArray(String s){
ArrayList<Character> ass = new ArrayList<>();
for(int i=0;i<s.length();i++)
ass.add(s.charAt(i));
return ass;
}
import java.util.Scanner;
import java.util.ArrayList;
public class book {
public static void main(String[] args)
{
ArrayList<Container> containers = new ArrayList<>();
containers.add(new Container());
System.out.print("Enter the number of the objects: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.print("Enter the weights of the objects: ");
for(int i=0;i<num;i++){
double tmp = input.nextDouble();
boolean in=false;
for(Container now : containers){
if(now.weightLeft>=tmp){
now.asses.add(tmp);
now.weightLeft-=tmp;
in=true;
break;
}
}
if(!in) {
Container jj = new Container();
jj.weightLeft-=tmp;
jj.asses.add(tmp);
containers.add(jj);
}
}
int index=1;
for(Container jojo:containers){
System.out.print("Container "+index+" contains objects with weight ");
index++;
for(double jj:jojo.asses)
System.out.print(jj+" ");
System.out.print('\n');
}
}
}
class Container{
public double weightLeft = 10;
public ArrayList<Double> asses;
public Container(){
asses = new ArrayList<>();
}
}