⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rootclass.java

📁 国外的数据结构与算法分析用书
💻 JAVA
字号:
import java.io.*;
import java.math.*;
import java.util.Random;

public class RootClass
{
    public static void main(String args[]) throws Exception
    {
    	BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    	System.out.println("1. Input from keyboard");
    	System.out.println("2. Use random number");
    	System.out.println("Enter your choice. 1 or 2");
    	int choice=Integer.parseInt(input.readLine());
    	if(choice==1)
        	testTemperature();
        else if(choice==2)
        	testTemperatureRandom();
        else 
        	System.out.println("Invalid input");

        System.out.println("\n");
		compareBoxesAndArrays();
    }
 
    /** Get the input from the keyboard*/
    static void testTemperature()
    {
    	BufferedReader consolInput = new BufferedReader(new InputStreamReader(System.in));
    	int[] recentYear = new int[20];
    	int sum=0, i;
    	for(i=0; i<recentYear.length; i++)
    	{
    		System.out.println("Enter value");
    		try{
    			recentYear[i]=Integer.parseInt(consolInput.readLine());
    		}catch(IOException e){
    			System.out.println("Cannot convert to the integer");
    		}
    		sum = sum + recentYear[i];
    	}
    	double recentMean = sum*1.0 / recentYear.length;
    	System.out.println("Average is: "+recentMean);
    	int[] category = new int[21];
    	int spot;
    	for(i=0; i<recentYear.length; i++)
    	{
    		//the full decimal 5.0 is needed to force it to do float division
    		spot = (int) Math.floor((recentYear[i] - recentMean + 2) / 5.0) + 10;
    		category[spot]++;
    	}

        for (i = 0; i < 21; i++)
            System.out.println((i-10) + " :  category : " + category[i]);
    }
    
    /** Use the random number as the input*/
    static void testTemperatureRandom()
    {
    	/* create a new random number generator */
    	Random rand = new Random(438561);
        int[] recentYear = new int[20];    
        int sum=0, i;
        for (i=0; i<20; i++)
        {
        	/* rand.nextInt(int n) return a pseudorandom, 
        	uniformly distributed int value between 0 
        	and n (exclude n) */
        	recentYear[i] = rand.nextInt(50) - 10;
            sum += recentYear[i];
        }
        double recentMean = sum*1.0 /20;
        
        int category[] = new int[21];
        int spot;
        for (i = 0; i < 20; i++)
        {
            spot = (int) Math.floor((recentYear[i] - recentMean - 2)/5.0) + 10;
            category[spot]++;
        }
        
        for (i = 0; i < 21; i++)
            System.out.println((i-10) + " :  category : " + category[i]);
    }
    
    static void compareBoxesAndArrays()
    {
        BasicBox box1, box2, box3, box4;
        box1 = new BasicBox(2, 4, 6);
        box2 = box1;
        box3 = new BasicBox(box1.length, box1.width, box1.height);
        System.out.println("box1==box2 has value " + (box1==box2));
        System.out.println("box1.equals(box2) has value " + (box1.equals(box2)));
        System.out.println("box1.equals(box3) has value " + (box1.equals(box3)));
        System.out.println("box1==box3 has value " + (box1==box3));
        
        BasicBox[] ar1, ar2, ar3;
        ar1 = new BasicBox[5];
        ar1[0] = box1;
        ar1[1] = box2;
        ar1[2] = box3;
        ar2 = ar1;
        box4 = new BasicBox(10, 20, 30);
        ar1[3] = box4;
        ar3 = (BasicBox[]) ar1.clone();
        System.out.println("ar1==ar2 has value " + (ar1==ar2));
        System.out.println("ar1==ar3 has value " + (ar1==ar3));
        System.out.println("ar1.equals(ar3) has value " + (ar1.equals(ar3)));
        ar3[3] = new BasicBox(10, 20, 30);
        System.out.println("Placing a new array in ar3[3]");
        System.out.println("ar1.equals(ar3) has value " 
                           + (ar1.equals(ar3)));
        System.out.println("ar1[3].equals(ar3[3]) has value " 
                           + (ar1[3].equals(ar3[3])));
   }
   
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -