Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% revision part 1
- public class ResistorCodes {
- public static void main(String[] args) {
- ResistorCodes p1 = new ResistorCodes();
- System.out.println(p1.ResistorValue("red","red","orange") + " ohms");
- System.out.println(p1.ResistorValue("violet","green","yellow") + " ohms");
- System.out.println(p1.ResistorValue("black","brown","brown") + " ohms");
- }
- private int ResistorValue(String col1, String col2, String col3) {
- int total = 0;
- total = (int) ((getVal(col1)*10 + getVal(col2))*Math.pow(10, getVal(col3)));
- return total;
- }
- private int getVal(String colour) {
- if (colour == "black")
- return 0;
- else if (colour == "brown")
- return 1;
- else if (colour == "red")
- return 2;
- else if (colour == "orange")
- return 3;
- else if (colour == "yellow")
- return 4;
- else if (colour == "green")
- return 5;
- else if (colour == "blue")
- return 6;
- else if (colour == "violet")
- return 7;
- else if (colour == "grey")
- return 8;
- else if (colour == "white")
- return 9;
- else
- return 0;
- }
- }
- %% revision part 2
- public class Chess {
- public static void main(String[] args) {
- Chess c1 = new Castle(1,2);
- System.out.println(c1);
- Chess c2 = new Knight(3,4);
- System.out.println(c2);
- }
- }
- class Castle extends Chess {
- private int x;
- private int y;
- @Override
- public String toString() {
- return "Castle at position: " + x + "," + y;
- }
- public Castle(int i, int j) {
- this.x = i;
- this.y = j;
- }
- }
- class Knight extends Chess {
- private int x;
- private int y;
- @Override
- public String toString() {
- return "Knight at position: " + x + "," + y;
- }
- public Knight(int i, int j) {
- this.x = i;
- this.y = j;
- }
- }
- %% revision part 3
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class stringToInt {
- public static void main(String[] args) throws FileNotFoundException {
- stringToInt st1 = new stringToInt();
- st1.runDemo();
- }
- private void runDemo() throws FileNotFoundException {
- int result = 0;
- Scanner sc = new Scanner(new FileInputStream("file.txt"));
- while (sc.hasNextLine()){
- String val1 = sc.nextLine();
- String val2 = sc.nextLine();
- String op = sc.nextLine();
- char oper = op.charAt(0);
- if(oper == '+'){
- result = Integer.parseInt(val1) + Integer.parseInt(val2);
- System.out.println(result);
- }
- else if(oper == '-'){
- result = Integer.parseInt(val1) - Integer.parseInt(val2);
- System.out.println(result);
- }
- else if(oper == '*'){
- result = Integer.parseInt(val1) * Integer.parseInt(val2);
- System.out.println(result);
- }
- else if(oper == '/'){
- result = Integer.parseInt(val1) / Integer.parseInt(val2);
- System.out.println(result);
- }
- }
- sc.close();
- }
- }
- %%revision part 4
- import java.text.DecimalFormat;
- public class Change {
- public static void main(String[] args) {
- Change cg = new Change();
- cg.changeBack(350);
- cg.changeBack(510);
- cg.changeBack(125);
- }
- private void changeBack(double cost) {
- try{
- if (cost > 500)
- throw new NotEnoughMoneyException();
- else if (cost <= 500){
- double money = 5.00;
- double decimalCost = cost / 100.0;
- double change = money - decimalCost;
- DecimalFormat dm = new DecimalFormat("0.00");
- System.out.println("You bought an item for $" + dm.format(decimalCost));
- System.out.println("Your change is $" + dm.format(change) + ":");
- int count = 0;
- if (change >= 2){
- count = (int) (change/2);
- change = change - 2*count;
- }
- System.out.println(count + " two dollar coins");
- count = 0;
- if (change >= 1){
- count = (int) (change/1);
- change = change - 1*count;
- }
- System.out.println(count + " one dollar coins");
- count = 0;
- if (change >= 0.5){
- count = (int) (change/0.5);
- change = change - 0.5*count;
- }
- System.out.println(count + " fifty cent coins");
- count = 0;
- if (change >= 0.2){
- count = (int) (change/0.2);
- change = change - 0.2*count;
- }
- System.out.println(count + " twenty cent coins");
- count = 0;
- if (change >= 0.1){
- count = (int) (change/0.1);
- change = change - 0.1*count;
- }
- System.out.println(count + " ten cent coins");
- change = Double.parseDouble(dm.format(change));
- count = 0;
- if (change >= 0.05){
- count = (int) (change/0.05);
- change = change - 0.05*count;
- }
- System.out.println(count + " five cent coins");
- System.out.println();
- }
- }
- catch (NotEnoughMoneyException e){
- System.out.println(e.getMessage());
- }
- }
- }
- class NotEnoughMoneyException extends Exception {
- /**
- *
- */
- private static final long serialVersionUID = 4257723934124110055L;
- public NotEnoughMoneyException()
- {
- super("You Do Not Have Enough Money!\n");
- }
- public NotEnoughMoneyException(String message)
- {
- super(message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement