Java使用printwriter对象

时间:2015-09-05 03:18:02

标签: java object methods constructor printwriter

我在这里遇到很多麻烦。我需要将代码与以下Driver class以及Decimal classMenu classBinary classHexadecimal class进行匹配。如下所示Driver class,我写了Decimal class。我知道它制作得很糟糕,但我遇到的最大问题是我不知道如何使用printwriter对象和其他方法将所有输出发送到csis.txt。 Eclipse中Decimal dec = new Decimal(pw);的感叹号表示不使用局部变量的值。另外,如何在Decimal.pw中使用Decimal class

这是我需要使用的Driver's class

import java.io.*;
import java.util.Scanner;

public class Driver {
    public static void main (String[] args) throws IOException{
        int choice;
        PrintWriter pw = new PrintWriter(new FileWriter("csis.txt"));
        Decimal dec = new Decimal(pw);
        Binary bin = new Binary(pw);
        Hexadecimal hex = new Hexadecimal(pw);
        Menu menu = new Menu(pw);

        do {
            menu.display();
            choice = menu.getSelection();
            switch (choice) {
                case 1: dec.decToBin(); break;
                case 2: dec.decToHex(); break;
                case 3: bin.binToDec(); break;
                case 4: bin.binToHex(); break;
                case 5: hex.hexToDec(); break;
                case 6: hex.hexToBin(); break;
            }
        }while (choice != 7);
        pw.print("Sends to output file.");
        pw.println("Sends to output file with linefeed.");
        pw.close();
    }
}

这是Decimal class

import java.util.Scanner;
import java.io.*;

public class Decimal{
    Scanner kb = new Scanner(System.in);
    int inDec;  
    int num;
    int rem;//remainder

    private PrintWriter pw;
    public Decimal(PrintWriter pw) {
        this.pw = pw;
    }

    //Allows user to type in a positive decimal number
    public void inputDec(){
        System.out.println("Enter a positive decimal number. I will convert into a binary number.");
        inDec = kb.nextInt();
        num = inDec;
    }

    //converts decimal into binary
    public void decToBin(){
        if (num < 0){
            System.out.println("Error. You should put a positive decimal number.");
        }
        else if (num == 0){
            System.out.println("0000 0000 0000 0000 0000 0000 0000");
        }
        else{
        int binary[] = new int[32];
        for(int j=0; num>0; j++){
            binary[j] = num%2;
            num = num/2;
        } 
        for(int j=31; j>=0; j--){
            System.out.print(binary[j]);
                if(j%4==0)
                    System.out.print(" ");
        }
        System.out.print("\n");
      }
    }

    //Allows user to type in a positive decimal number
    public void inputHex(){
        System.out.println("Enter a positive decimal number. I will convert into a hexadecimal number.");
        inDec = kb.nextInt();
        num = inDec;
    }

    //converts decimal into hexadecimal
    public void decToHex(){
         String str=""; 
         char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

         while(num>0)
         {
           rem=num%16; 
           str=hex[rem]+str; 
           num=num/16;
         }
         System.out.println(str);
    }
}

0 个答案:

没有答案