d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > User Blogs > Assignment Log
123Next
Add Reply New Topic
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Mar 26 2013 08:01pm
Various assignments I've completed for my classes.
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Mar 26 2013 08:04pm
111 Assignment#2: Prints some data for different shapes using math class for calculation

Code
public class YuepA02
{
public static void main(String [] args)
{
 double r = 2.5;
 double h = 6.0;
 double r2 = Math.pow(r, 2);
 double h2 = Math.pow(h, 2);
 
 
 System.out.println("For a cone with a radius of " + r + " and a Height of " + h);
   System.out.println("Slant Height = " + Math.sqrt(r2 + h2));
  System.out.println("Volume = " + (((Math.PI) * (r2) * (h))/3));
   System.out.println("Lateral Face Area = " + Math.PI * r * Math.sqrt(r2 + h2));
    System.out.println("Total Surface Area = " + (Math.PI * r2 + Math.PI * r * Math.sqrt(r2 + h2)));
}
}


This post was edited by bakalolo on Mar 26 2013 08:04pm
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Mar 26 2013 08:06pm
111 Assignment#3: Convert feet/meter

Code
//Asignment #04
//
//
//


import java.util.Scanner;
import java.util.InputMismatchException;

public class yuepA04
{


public static void main (String[] args)
{

try{


Scanner keybd = new Scanner (System.in);


System.out.println ("Please enter a distance to convert:");
 
double x = keybd.nextDouble();
 
 
 
 
System.out.println(x + " feet(s) = " + x*0.3048 + " meter(s)");
System.out.println(x + " meter(s) = " + x/0.3048 + " feet(s)");

}catch (InputMismatchException ime)
 {
 System.out.println("Please enter a numeric value.");
 }
}


}
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Mar 26 2013 08:07pm
111 A4

Code
import java.util.Scanner;
import java.util.InputMismatchException;
public class YuepA04
{
public static void main(String[] args)
{

 
 Scanner keybd = new Scanner (System.in);
 try{
 //asking imput
 System.out.println("Enter the radius of a cone: ");
 double r = keybd.nextDouble();
 
 System.out.println("Enter the height of a cone: ");
 double h = keybd.nextDouble();
 
 //method calls
 System.out.print("Slant Height = ");
 double SH = Cone.getSlantHeight(r, h);
 System.out.println(SH);
 
 System.out.print("Volume = ");
 double V = Cone.getVolume(r, h);
 System.out.println(V);
   
 System.out.print("Lateral Surface Area =  ");
 double LA = Cone.getLateralSurfaceArea(r, h);
 System.out.println(LA);
     
 System.out.print("Total Surface Area =  ");
 double TA = Cone.getTotalSurfaceArea(r, h);
 System.out.println(TA);
 
 }catch (InputMismatchException ime)
  {
  System.out.println ("Please enter a numberic value.");
  }

}
}

class Cone
{
static double SlantHeight, Volume, LateralSurfaceArea, TotalSurfaceArea;


public static double getSlantHeight(double r, double h)
{
 
 double r2 = Math.pow(r, 2);
 double h2 = Math.pow(h, 2);
 SlantHeight = Math.sqrt(r2 + h2);
 return SlantHeight;
}

public static double getVolume(double r, double h)
{
 double r2 = Math.pow(r, 2);
 double h2 = Math.pow(h, 2);
 Volume = (((Math.PI) * (r2) * (h))/3);
 return Volume;
}
public static double getLateralSurfaceArea(double r, double h)
{
 
 double r2 = Math.pow(r, 2);
 double h2 = Math.pow(h, 2);
 LateralSurfaceArea = Math.PI * r * Math.sqrt(r2 + h2);
 return LateralSurfaceArea;
 
}
public static double getTotalSurfaceArea(double r, double h)
{
 double r2 = Math.pow(r, 2);
 double h2 = Math.pow(h, 2);
 TotalSurfaceArea = Math.PI * r2 + Math.PI * r * Math.sqrt(r2 + h2);
 return TotalSurfaceArea;
}
}
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Mar 26 2013 08:07pm
111 A5

Code
import java.util.Scanner;
import java.util.InputMismatchException;

/**
* Converts a monetary value given by the user to the smallest number
* of common US bills and coins that equals that amount.
*
*
*/
public class YuepA05 {

 public static void main(String[] args) {
   try {
     Scanner keybd = new Scanner(System.in);

     //get amount to convert
     System.out.print("Enter a US dollar amount: ");
     double amount = keybd.nextDouble();

     //convert to bills and coins
     int bills = (int) amount;
 
 //double coins = ((amount - bills) * 100);

     int coins = (bills * 100) - ((int) Math.rint((amount) * 100));

     //convert bills
     int twenties = bills / 20;
     bills %= 20;
     int tens = bills / 10;
     bills %= 10;
     int fives = bills / 5;
     bills %= 5;
     int ones = bills;

     //convert coins
     int quarters = coins / -25;
     coins %= 25;
     int dimes = coins / -10;
     coins %= 10;
     int nickels = coins / -5;
     coins %= 5;
     int pennies = -coins;

     //print results
     System.out.println("$" + amount + " in the fewest number of bills and coins: ");
     System.out.println(twenties + " x $20 bills");
     System.out.println(tens + " x $10 bills");
     System.out.println(fives + " x $5 bills");
     System.out.println(ones + " x $1 bills");
     System.out.println(quarters + " quarters");
     System.out.println(dimes + " dimes");
     System.out.println(nickels + " nickels");
     System.out.println(pennies + " pennies");

   }catch (InputMismatchException ime) {
     System.out.println("You must enter a number.  Please try again.");
   }
 }
}
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Mar 26 2013 08:08pm
111 A6

Code
import java.util.Scanner;
import java.util.InputMismatchException;


public class YuepA06
{
public static void main (String[] args)
{
int x, y, z;

try {
 Scanner keybd = new Scanner (System.in);
 System.out.println("Please enter 3 integers");
 
 int a = keybd.nextInt();
 int b = keybd.nextInt();
 int c = keybd.nextInt();
 
 //compare the 1st input(a) and 2nd input(b)
 //sets the greater value to x and smaller value to y
 //so that x > y
 if (a > b)
 {
  x = a;
  y = b;
  z = c;
 }
 
 else
   {
  x = b;
  y = a;
  z = c;
 }
 
 //compare y from above to last input(c)
 //sets z = c if y is bigger or assign z to previous y value
 //and assign y to c if y is smaller than c. z = smallest input
 if (y > z)
 {
  z = c;
 }
 else
  {
  z = y;
  y = c;
 }
 
 //set new variables for current value of x, y  so I can switch values of
 //x and y if nessesary
 int d = x;
 int e = y;
 
 
 if (!(x > y))//switch the value of x and y if y is bigger
 {
 d = y;
 e = x;
 System.out.println("Sorted lowest-to-highest: " + z + " " + e + " " + d);
 System.out.println("Sorted highest-to-lowest: " + d + " " + e + " " + z);
 }
 else
 {
 System.out.println("Sorted lowest-to-highest: " + z + " " + e + " " + d);
 System.out.println("Sorted highest-to-lowest: " + d + " " + e + " " + z);

 }  
}catch (InputMismatchException ime)
 {
  System.out.println("ERROR: Please enter integer values(whole numbers).");
 }
}

}
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Mar 26 2013 08:09pm
111 A7

Code
import java.util.Scanner;
import java.util.InputMismatchException;

public class YuepA07
{
public static void main (String[] args)
{
 int month = 28;
   int year = 364;
 String dotw = "Festival.";
 //String pred = " Neutral";
 String work = " Work";
 String tremor =" Tremor";
 String eclipse = " Eclipse";
 String collection = " Collection";
 String grog = " Grog";
 String flog = "";

 
 int l, u, id;
 
 Scanner keybd = new Scanner (System.in);
 
 try {
 System.out.print("Enter the current Bleegian DAY of the month: ");

 int d = keybd.nextInt();
 
 if ((d <= 27) && (d >= -27))
  {
      if (d < 0)
 {
 u = d + 28;
 d = u;
 }
  if ( (d == 7) || (d == 14) || (d == 21) ||(d == -7) || (d == -14) || (d == -21) )
  dotw = "Sunday";
  else if ( (d == 1) || (d == 8) || (d == 15) || (d == 22) ||(d == -6) || (d == -13) || (d == -20) || (d == -27))
  dotw = "Monday";
  else if ( (d == 2) || (d == 9) || (d == 16) || (d == 23) ||(d == -5) || (d == -12) || (d == -19) || (d == -26))
  dotw = "Tuesday";
  else if ( (d == 3) || (d == 10) || (d == 17) || (d == 24) ||(d == -4) || (d == -11) || (d == -18) || (d == -25))
  dotw = "Wednesday";
  else if ( (d == 4) || (d == 11) || (d == 18) || (d == 25)||(d == -3) || (d == -10) || (d == -17) || (d == -24) )
  dotw = "Thursday";
  else if ( (d == 5) || (d == 12) || (d == 19) || (d == 26) ||(d == -2) || (d == -9) || (d == -16) || (d == -23))
  dotw = "Friday";
  else if ( (d == 6) || (d == 13) || (d == 20) || (d == 27) || (d == -15) || (d == -1) || (d == -8) || (d == -22) )  
    dotw = "Saturday";
  }
 
 else
  {
  System.out.println("Not a valid DAY of the month.");
  return;
  }
 
 
 if ( d == 0)
 work = "";
 
 int t = d %2;
 if ( t == 0)
 tremor = "";
 
 int e = ((d - 1) % 4);
 if (e != 0)
 eclipse = "";
 
 int c = ((d - 2) % 4);
 if (c != 0)
 collection = "";
 
 if ((dotw != "Wednesday") && (d != 0))
 grog = "";
 
 if ((e == 0) && (dotw != "Wednesday") && (dotw != "Festival"))
 flog = " Flogging";


 
 System.out.println("Today (day " + d + ") is " + dotw);
 System.out.println("Today will be marked by:" + work + tremor + eclipse + collection + grog + flog);
 
 /*
 System.out.print("Enter a Bleegian MONTH: ");
 
 int m = keybd.nextInt();
 int total = (m * 28) + d;
 
 
 if ( (m >= 0) && (m <= 12) )
 {
 System.out.println("Day " + d + " of Month " + m + " = Day " + total + " of the year.");
 }
 
 else
 {
 System.out.println("Not a valid MONTH of the year.");
 return;
 }
 
 System.out.println("It is a " + dotw);
 l = total % 7;
 u = total % 4;
 id = total % 13;
 
 if ( (l == 0) && (u == 0) && (id == 0) )
   pred = "Momentous";
    else if ( (u == 0) && (id == 0) )
 pred = "Cursed";
    else if ( (id == 0) && (l == 0) )
 pred = "Blessed";
   
     else if ( (id == 0) )
 pred = "indeterminate";
   else if ( (l == 0) && (u == 0) )
 pred = pred;
   else if (u == 0)
 pred = "Unlucky";
   else if (l == 0)
 pred = "Lucky";
 
 
 System.out.println("The day will be: " + pred);
 */
 
 }catch (InputMismatchException ime)
 {
  System.out.println("ERROR: PLEASE ENTER INTEGER VALUES (WHOLE NUMBERS) ONLY.");
 }

}
}
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Mar 26 2013 08:09pm
111 A8

Code
import java.util.Scanner;

public class YuepA08
{
public static void main (String[] args)
{
 String mutation1, mutatione, mA, mB, mC, mD, mX, mY, mZ, fileName, ext, dext;
 

 Scanner keybd = new Scanner(System.in);
 
 System.out.print("Please enter a filename to convert: ");
 
 //asks for input(x) and trims it(t)
 String x = keybd.nextLine();
 
 String t = x.trim();
 
 int length = t.length();
 //check length of t if 0 then error
 
 if ( length == 0 ){
 System.out.println("Error: Didn't enter a file name to convert.");
 return;
 }
 //check first char, if '.' then error
 else if ( length >= 1) {
 char firstChar = t.charAt(0);
  if (firstChar == '.') {
  System.out.println("File can't start with '.' ");
  return;
  }
 }
 
 System.out.println("File Name: " + t);
 int dot = t.lastIndexOf('.');
 //if no dot, print t
 
 if (dot < 0) {
  fileName = t;
  ext = "";
  dext = "";
 System.out.println("Name only: " + fileName);
 System.out.println("Extension: " + ext);
 }
 //if dot, print filename(b4 dot) & ext(after dot)
 else {
 fileName = t.substring(0, dot);
 ext = t.substring(dot + 1);
 dext = t.substring(dot);
 
 System.out.println("Name only: " + fileName);
 System.out.println("Extension: " + ext);
 }
 
 //name manipulations--------------------
 mutation1 = fileName.replace(' ', '_');
 

 int m1Length = mutation1.length();
 
 if (m1Length > 8) {
 mA = mutation1.substring(0, 8);
 }
 else {
 mA = mutation1;
 }
 
 mB = mA.replace('.', '_');
 mC = mB.toUpperCase();
 
 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 //extension manipulations
 mutatione = dext.replace(' ', '_');


 int meLength = mutatione.length();
 
 if (meLength > 4) {
 mX = mutatione.substring(0, 4);
 }
 else {
 mX = mutatione;
 }

 mY = mX.toUpperCase();
 
 //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
 System.out.println("8.3 Filename: " + mC + mY);
 
}
}
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Mar 26 2013 08:10pm
111 A9

Code
import java.util.Scanner;
import java.util.Random;


public class YuepA09
{
public static void main (String args[])
{
 
 int player = 0;
 int b = 0;
 int comp = 0;
 int playerMove = 99;
 
 
 Scanner keybd = new Scanner(System.in);
 // Random computerInput = new Random();
 
 //generate a random number (0-2) for computer move
 //0 = rock, 1 = paper, 2 = scissor
 
 
 //int cmpMove = computerInput.nextInt(3);
 
 
 int a = 1;
 while (a != 0)
 {
 while (a != 0)
 {
 
 Random computerInput = new Random();
 int cmpMove = computerInput.nextInt(3);
 
 System.out.println("");
 System.out.println("Please enter your move (rock, paper, or scissor) or press Enter to quit:");
   String playerInput = keybd.nextLine();
 a = playerInput.length();
 //turns player input(string) into int, rock = 0, paper = 1, scissor = 2
 if (playerInput.equals("rock"))
 {
  playerMove = 0;
 }
 else if (playerInput.equals("scissor"))
 {
  playerMove = 2;
 }
 else if (playerInput.equals("paper"))
 {
  playerMove = 1;
 }
 else if (a == 0)
 {
  break;
 }
 else
 {
  System.out.println("Not a valid input.  Please check your spelling.");
  break;
 }
 
 
 //System.out.println("You have chosen " + playerInput);
 
 if (cmpMove == 0)
 {
  System.out.println("Computer move: ROCK.");
 }
 if (cmpMove == 1)
 {
  System.out.println("Computer move: PAPER.");
 }
 if (cmpMove == 2)
 {
  System.out.println("Computer move: SCISSOR.");
 }

 //method call
 compare game = new compare();
 int winz = game.whoWins(playerMove, cmpMove);
 
 if (winz == 1)
 {
  player++;
 }
 if (winz == 2)
 {
  comp++;
 }
 
 String whoWon = getWinner(winz);
 System.out.println(whoWon);
 b++;
 System.out.println(b + " Game(s) played so far : Your wins " + player + "     Computer wins " + comp + "     Ties " + (b - (player + comp)));
 System.out.println("___________________________________________________________________");
 }
 }
}
private static String getWinner(int winz)
{
  String outCome = "zzz";
  if (winz == 0)
  {
   outCome = "It's a TIE!";
  }
  if (winz == 1)
  {
   outCome = "YOU WON!";
  }
  if (winz == 2)
  {
   outCome = "COMPUTER WON!.";
  }

  return outCome;
}

}

class compare
{
private int computer = 0;
private int player = 0;
int win = 0;
static String Winner;

//method takes in 2 integers playerMove and cmpmove
//compares them, and return a int win where 0 = tie, 1 = player wins
//2 = computer wins.
public int whoWins(int playerMove, int cmpMove)
{
 //---------------if same move---------------------------
 if ((playerMove == 0) && (cmpMove == 0))
 {
  computer++;
  player++;
 }
 if ((playerMove == 1) && (cmpMove == 1))
 {
  computer++;
  player++;
 }
 if ((playerMove == 2) && (cmpMove == 2))
 {
  computer++;
  player++;
 }
 //-------------if player chose rock--------------------------  
 if ((playerMove == 0) && (cmpMove == 1))
 {
  computer++;
 }
 
 if ((playerMove == 0) && (cmpMove == 2))
 {
  player++;
 }
 //-----------------if player chose paper---------------------
 if ((playerMove == 1) && (cmpMove == 0))
 {
  player++;
 }
 if ((playerMove == 1) && (cmpMove == 2))
 {
  computer++;
 }
 //--------------------if player chose scissor-----------------
 if ((playerMove == 2) && (cmpMove == 0))
 {
  computer++;
 }
 
 if ((playerMove == 2) && (cmpMove == 1))
 {
  player++;
 }
 //------------returns the winner as int--------------
 if (player == computer)
 {
  win = 0;
 }
 if (player > computer)
 {
  win = 1;
 }
 if (player < computer)
 {
  win = 2;
 }

 return win;
}
}
Member
Posts: 20,928
Joined: Mar 18 2009
Gold: 435,910.13
Mar 26 2013 08:11pm
111 A10

Code

   public class YuepA10
  {
      public static void main (String args[])
     {
     
        System.out.println("Dimensions of 3 ice cream cones (as % of Original): ");
     
     //----original cone--------------------
        Cone small = new Cone(3, 12);
        double sVolume = small.getVolume();
        double sLateral = small.getLateralSurfaceArea();
     
        System.out.println("ORIGINAL:");
        System.out.println("Ice cream volume: " + sVolume + " cm^3 (100%)"  );
        System.out.println("Waffle cone area: " + sLateral + " cm^3 (100%)"  );
        System.out.println("Ice cream to cone ratio: " + (sVolume/sLateral) + " (100%)"  );
     
     //-----small cone-------------------
        Cone medium = new Cone(2.5, 10);
        double mVolume = medium.getVolume();
        double mLateral = medium.getLateralSurfaceArea();
     
        double q = Math.rint((mVolume/sVolume) * 100);
        double w = Math.rint((mLateral/sLateral) * 100);
        double e = Math.rint(((mVolume/mLateral)/(sVolume/sLateral) * 100));
     
        System.out.println("SMALL:");
        System.out.println("Ice cream volume: " + mVolume + " cm^3 " + "(" + (int)q + "%)" );
        System.out.println("Waffle cone area: " + mLateral + " cm^3 )" + "(" + (int)w + "%)" );
        System.out.println("Ice cream to cone ratio: " + (mVolume/mLateral) + " (" + (int)e + "%)"  );
     
     //------large cone-------------------
        Cone large = new Cone(4, 16);
        double lVolume = large.getVolume();
        double lLateral = large.getLateralSurfaceArea();
     
        double r = Math.rint((lVolume/sVolume) * 100);
        double t = Math.rint((lLateral/sLateral) * 100);
        double y = Math.rint(((lVolume/lLateral)/(sVolume/sLateral) * 100));
     
        System.out.println("LARGE:");
        System.out.println("Ice cream volume: " + lVolume + " cm^3 " + "(" + (int)r + "%)" );
        System.out.println("Waffle cone area: " + lLateral + " cm^3 " + "(" + (int)t + "%)" );
        System.out.println("Ice cream to cone ratio: " + (lVolume/lLateral) + " (" + (int)y + "%)"  );
     
     
     
     
     
     }
  }
  class Cone
  {
     private double Radius, Height, SlantHeight, Volume, LateralSurfaceArea, TotalSurfaceArea;
 
      public Cone(double r, double h)
     {
        Radius = r;
        Height = h;
     
     }
      public double getRadius()
     {
        return Radius;
     }
      public void setRadius(double r)
     {
        Radius = r;
     }
 
      public double getHeight()
     {
        return Height;
     }
      public void setHeight(double h)
     {
        Height = h;
     }
   
      public double getSlantHeight()
     {
     
        double r2 = Math.pow(Radius, 2);
        double h2 = Math.pow(Height, 2);
        SlantHeight = Math.sqrt(r2 + h2);
        return SlantHeight;
     }
 
      public double getVolume()
     {
        double r2 = Math.pow(Radius, 2);
        double h2 = Math.pow(Height, 2);
        Volume = (((Math.PI) * (r2) * (Height))/3);
        return Volume;
     }
      public double getLateralSurfaceArea()
     {
     
        double r2 = Math.pow(Radius, 2);
        double h2 = Math.pow(Height, 2);
        LateralSurfaceArea = Math.PI * Radius * Math.sqrt(r2 + h2);
        return LateralSurfaceArea;
     
     }
      public double getTotalSurfaceArea()
     {
        double r2 = Math.pow(Radius, 2);
        double h2 = Math.pow(Height, 2);
        TotalSurfaceArea = Math.PI * r2 + Math.PI * Radius * Math.sqrt(r2 + h2);
        return TotalSurfaceArea;
     }
  }
Go Back To User Blogs Topic List
123Next
Add Reply New Topic