Java HW

민겸·2023년 4월 11일
0

Schoolworks

목록 보기
5/5

1

import java.util.Scanner;

public class Counter {
    private int count; // 카운트 값을 저장할 인스턴스 변수

    // 생성자
    public Counter() {
        count = 0; //count를 0으로 초기화
    }

    // counter를 0으로 설정
    public void setZero() {
        count = 0;
    }

    // count 1 증가
    public void increment() {
        count++;
    }

    // count 1 감소
    public void decrement() {
        if (count > 0) {
            count--;
        }
    }

    // 현재 count 값을 가져옴
    public int countIs() {
        return count;
    }

    // count 값을 출력
    public void printCounter() {
        System.out.println("Count: " + count);
    }
}

2

import java.util.Scanner;

/**
 * Class for the purchase of one kind of item, such as 3 oranges.
 * Prices are set supermarket style, such as 5 for $1.25.
 */
public class Purchase {
    private String name;
    private int groupCount; // Part of price, like the 2 in 2 for $1.99.
    private double groupPrice;// Part of price, like the $1.99
    // in 2 for $1.99.
    private int numberBought; // Number of items bought.

    public void setName(String newName) {
        name = newName;

    }

    /**
     * Sets price to count pieces for $costForCount.
     * For example, 2 for $1.99.
     */
    public void setPrice(int count, double costForCount) {
        if ((count <= 0) || (costForCount <= 0)) {
            System.out.println("Error: Bad parameter in setPrice.");
            System.exit(0);
        } else {
            groupCount = count;
            groupPrice = costForCount;
        }
    }

    public void setNumberBought(int number) {
        if (number <= 0) {
            System.out.println("Error: Bad parameter in setNumberBought.");
            System.exit(0);
        } else
            numberBought = number;
    }

    /**
     * Reads from keyboard the price and number of a purchase.
     */
    public void readInput() {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter name of item you are purchasing:");
        name = keyboard.nextLine();
        System.out.println("Enter price of item as two numbers.");
        System.out.println("For example, 3 for $2.99 is entered as");
        System.out.println("3 2.99");
        System.out.println("Enter price of item as two numbers, now:");
        groupCount = keyboard.nextInt();
        groupPrice = keyboard.nextDouble();
        while ((groupCount <= 0) || (groupPrice <= 0)) { // Try again:
            System.out.println("Both numbers must be positive. Try again.");
            System.out.println("Enter price of item as two numbers.");
            System.out.println("For example, 3 for $2.99 is entered as");
            System.out.println("3 2.99");
            System.out.println("Enter price of item as two numbers, now:");
            groupCount = keyboard.nextInt();
            groupPrice = keyboard.nextDouble();
        }
        System.out.println("Enter number of items purchased:");
        numberBought = keyboard.nextInt();
        while (numberBought <= 0) { // Try again:
            System.out.println("Number must be positive. Try again.");
            System.out.println("Enter number of items purchased:");
            numberBought = keyboard.nextInt();
        }
    }

    /**
     * Displays price and number being purchased.
     */
    public void writeOutput()

    {
        System.out.println(numberBought + " " + name);
        System.out.println("at " + groupCount +
                " for $" + groupPrice);
    }

    public String getName() {
        return name;
    }

    public double getTotalCost() {
        return (groupPrice / groupCount) * numberBought;
    }

    public double getUnitCost() {
        return groupPrice / groupCount;
    }

    public int getNumberBought() {
        return numberBought;
    }
}

3

import java.util.Scanner;

public class ConcertPromoter {
    private String bandName;
    private int venueCapacity;
    private int ticketsSold;
    private double phoneTicketPrice;
    private double venueTicketPrice;
    private double totalSales;

    // Constructor
    public ConcertPromoter() {
        bandName = "";
        venueCapacity = 0;
        ticketsSold = 0;
        phoneTicketPrice = 0.0;
        venueTicketPrice = 0.0;
        totalSales = 0.0;
    }

    // Initialize the concert promoter with band name, venue capacity, phone ticket price, and venue ticket price
    public void initialize(String bandName, int venueCapacity, double phoneTicketPrice, double venueTicketPrice) {
        this.bandName = bandName;
        this.venueCapacity = venueCapacity;
        this.phoneTicketPrice = phoneTicketPrice;
        this.venueTicketPrice = venueTicketPrice;
    }

    // Record the sale of one or more tickets
    // Preconditions: numTickets > 0
    // Postconditions: ticketsSold is increased by numTickets, totalSales is updated
    public void doTicketSale(int numTickets) {
        ticketsSold += numTickets;
        totalSales += phoneTicketPrice * numTickets;
    }

    // Change from phone sales to sales at the concert venue
    // Postconditions: phoneTicketPrice is set to venueTicketPrice
    public void phoneSalesOver() {
        phoneTicketPrice = venueTicketPrice;
    }

    // Return the number of tickets sold
    // Postconditions: None
    public int getTicketsSold() {
        return ticketsSold;
    }

    // Return the number of tickets remaining
    // Postconditions: None
    public int getTicketsLeft() {
        return venueCapacity - ticketsSold;
    }

    // Return the total sales for the concert
    // Postconditions: None
    public double getSalesReport() {
        return totalSales;
    }

    // Check if sales are still phone sales only
    // Postconditions: None
    public boolean phoneSalesOnly() {
        return phoneTicketPrice == venueTicketPrice;
    }

    public static void main(String[] args) {
        ConcertPromoter concert = new ConcertPromoter();
        concert.initialize("The Ducks", 100, 10.00, 12.00);
        System.out.println("Concert Promoter Sales Program started");
        Scanner reader = new Scanner(System.in);
        boolean done = false;
        while (!done) {
            if (concert.phoneSalesOnly())
                System.out.println("Sell tickets (S), Change to venue sales (V), Finish selling (F)");
            else
                System.out.println("Sell tickets (S), Finish selling (F)");
            String response = reader.next();
            if (response.equals("F"))
                done = true;
            else if (response.equals("S")) {
                System.out.print("Enter number of tickets to sell: ");
                int numTickets = reader.nextInt();
                concert.doTicketSale(numTickets);
            } else if (response.equals("V"))
                concert.phoneSalesOver();
            else {
                System.out.println("Sorry the response " + response + " was not valid.");
            }
            System.out.println("Tickets remaining: " + concert.getTicketsLeft());
        }
        System.out.println("Final report: " + concert.getSalesReport());
    }
}

4

import java.util.Scanner;

public class GradesGraph {
    private int numberOfAs; // A 학점의 수
    private int numberOfBs; // B 학점의 수
    private int numberOfCs; // C 학점의 수
    private int numberOfDs; // D 학점의 수
    private int numberOfFs; // F 학점의 수

  // Set the number of each of the letter grades A, B, C, D, and F.
  public void set(int numberOfAs, int numberOfBs, int numberOfCs, int numberOfDs, int numberOfFs) {
    this.numberOfAs = numberOfAs;
    this.numberOfBs = numberOfBs;
    this.numberOfCs = numberOfCs;
    this.numberOfDs = numberOfDs;
    this.numberOfFs = numberOfFs;
}

// Read the number of each of the letter grades A, B, C, D, and F.
public void readInput() {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the number of A grades: ");
    numberOfAs = input.nextInt();
    System.out.print("Enter the number of B grades: ");
    numberOfBs = input.nextInt();
    System.out.print("Enter the number of C grades: ");
    numberOfCs = input.nextInt();
    System.out.print("Enter the number of D grades: ");
    numberOfDs = input.nextInt();
    System.out.print("Enter the number of F grades: ");
    numberOfFs = input.nextInt();
}

    // A 학점의 수 설정
    public void setAcount(int count) {
        numberOfAs = count;
    }

    // B 학점의 수 설정
    public void setBcount(int count) {
        numberOfBs = count;
    }

    // C 학점의 수 설정
    public void setCcount(int count) {
        numberOfCs = count;
    }

    // D 학점의 수 설정
    public void setDcount(int count) {
        numberOfDs = count;
    }

    // F 학점의 수 설정
    public void setFcount(int count) {
        numberOfFs = count;
    }

    // A 학점의 수 반환
    public int getAcount() {
        return numberOfAs;
    }

    // B 학점의 수 반환
    public int getBcount() {
        return numberOfBs;
    }

    // C 학점의 수 반환
    public int getCcount() {
        return numberOfCs;
    }

    // D 학점의 수 반환
    public int getDcount() {
        return numberOfDs;
    }

    // F 학점의 수 반환
    public int getFcount() {
        return numberOfFs;
    }

    // 전체 학점의 수 반환
    public int getTotalNumberOfGrades() {
        return numberOfAs + numberOfBs + numberOfCs + numberOfDs + numberOfFs;
    }

    // A 학점의 백분율 반환
    public int getPercentA() {
        return (int) (((double) numberOfAs / getTotalNumberOfGrades()) * 100);
    }

    // B 학점의 백분율 반환
    public int getPercentB() {
        return (int) (((double) numberOfBs / getTotalNumberOfGrades()) * 100);
    }

    // C 학점의 백분율 반환
    public int getPercentC() {
        return (int) (((double) numberOfCs / getTotalNumberOfGrades()) * 100);
    }

    // D 학점의 백분율 반환
    public int getPercentD() {
        return (int) (((double) numberOfDs / getTotalNumberOfGrades()) * 100);
    }

    // F 학점의 백분율 반환
    public int getPercentF() {
        return (int) (((double) numberOfFs / getTotalNumberOfGrades()) * 100);
    }

    // 학점 분포를 막대 그래프로 출력
    public void drawGradeDistribution() {
        int maxPercentage = 100; // 가장 높은 백분율 (100%)
        int scale = 2; // 1%를 나타내는 '*'의 개수
        int maxAster

0개의 댓글