Showing posts with label Class. Show all posts
Showing posts with label Class. Show all posts

Creates a class that compresses the file that is inputted

How to write a Java Program to Creates a class that compresses the file that is in-putted ?


Solution:

//Creates a class that compresses the file that is inputted
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Compress {
 //Creates a static int type to store the default ASCII values that will be appended
 static int defaultValues = 256;
 //Creates a variable that reads the current byte in the file
 int readByte = 0;
 //Creates a constant int type to store the size of the dictionary/hash table
 final static int MAX_TABLE_SIZE = 4096;
 public static void main (String[] args) throws IOException
 {
   
  String key = "";
  //Creates a new Dictionary and initializes it with the set size
  Dictionary newDict = new Dictionary(MAX_TABLE_SIZE);
  //String stringValue  = new Character((char)).toString();
  int readByte;
  MyOutput fileOut = new MyOutput();
  int appendCode = 256;
  String inputFile = args[0];
  String outputFile = inputFile + ".zzz";
  try
  {
   BufferedInputStream in;
   in = new BufferedInputStream(new FileInputStream(inputFile));
   BufferedOutputStream out;
   out = new BufferedOutputStream(new FileOutputStream(outputFile));
   for (int i = 0; i < defaultValues; i++)
   {
    String stringValue2  = new Character((char)i).toString();
    DictEntry newEntry = new DictEntry(stringValue2, i);
    newDict.insert(newEntry);
   } 
   readByte=in.read();
   String appendString = "";
   //String value = new Character((char)readByte).toString();
   while (in.read()!=-1) //&& newDict.find(appendString).equals(null))
   {
    String value = new Character((char)readByte).toString();
    appendString += value;
    //appendString += (char)readByte;
    /*if (readByte == -1)
    {
     System.out.println("");
     break;
    }*/
    if (newDict.find(appendString) == null || newDict.find(appendString).equals(null))
    {
     //appendString += value;
     DictEntry newString = new DictEntry(appendString, appendCode);
     if (newDict.numElements() < MAX_TABLE_SIZE)
     {
      newDict.insert(newString);
     }
     appendCode++;
     //appendString += (char)readByte;//Integer.toString(appendCode);
     //fileOut.output(appendCode, out);
     //appendString = "";
     fileOut.output(appendCode, out); //outputkey
     appendString = "";
     //System.out.print(newDict.find(appendString).getCode());
    }
    else {
     key = appendString;
     readByte = in.read();
    }
    appendString = value;
    //readByte = in.read();
   }
   //readByte = in.read();
   fileOut.output(appendCode, out);//out put key again
   //fileOut.flush(out);
   if (in != null)
   {
    in.close();
   }
   //fileOut.flush(out);
   if (out != null)
   {
    out.close();
   } 
   fileOut.flush(out);
  }
  catch (DictionaryException e)
  {
   System.out.println("error");
  }
 }

}

Java Rock Paper Scissors


//Import Scanner
import java.util.Scanner;
import java.util.Random;
public class PaperScissorsRock
{
        public static void main(String[] args)
        {
             //Title
             System.out.println("Rock, Paper, Scissors");
             //Scanner
             Scanner sc = new Scanner(System.in);
             //Insert Values
             System.out.print("Enter 0 for paper, 1 for scissors, or 2 for rock (-1 to quit): ");
             int Value1 = sc.nextInt();
             //Randomizer
             Random randomGen = new Random();
             // nextInt() returns a pseudo random, uniformly
             // distributed integer value between 0 (inclusive)
             // and the specified value (exclusive), therefore,
             // the following generates a random value between 0 to 2
             int Value2 = randomGen.nextInt(3);
             //Computer Output
             switch(Value2)
             {
             case 0:
             System.out.println("Computer picks paper");
             break;
             case 1:
                System.out.println("Computer picks scissors");
                break;
             case 2:
             System.out.println("Computer picks rock");
                break;
             }
             //Calculation
             if (Value1 >= 0){
                if ((Value1 == 0 && Value2 == 2) || (Value1 == 1 && Value2 == 0) || (Value1 == 2 && Value2 == 1))
                      System.out.println("Player Wins");
                if ((Value1 == 0 && Value2 == 0) || (Value1 == 1 && Value2 == 1) || (Value1 == 2 && Value2 == 2))
                    System.out.println("Draw");   
             else 
             System.out.println("Computer Wins");
             }
         //Error
         if (Value1 < 0)
              System.out.println("Invalid input.");
             //Output
         }
}

JAVA Leap Year WIP


//Import Scanner
import java.util.Scanner;
public class LeapYears
{
        public static void main(String[] args)
        {
                    //Title
                    System.out.println("Leap Year Calculation");
                //Scanner
                Scanner sc = new Scanner(System.in);
                //Insert Values
                System.out.print("Enter The Year: ");
                int Year = sc.nextInt();
                //Calculation
                int LeapYear = Year % 4;
                int LeapYear2 = Year % 100;
                int LeapYear3 = Year % 400;
                //Output
                if (LeapYear > 0)
                 if ((LeapYear == 0 && LeapYear2 != 0) || LeapYear3 == 0)
                 {
                       System.out.println(Year + " is a leap year");
                          System.out.println("The numbers of days in" + Year + "is 366.");
                }
                else {
                       System.out.println(Year + " is NOT a leap year");
                       System.out.println("The numbers of days in" + Year + "is 365.");         
                }
                //Output Invalid
                if (Year <= 0) {
                 System.out.println("Invalid Input");
                }
                //Closing
                }

Java Weight Program


//Import Scanner
import java.util.Scanner;
public class WeightProgram
{
        public static void main(String[] args)
        {
             //Title
             System.out.println("Weight Program ");
                //Scanner
                Scanner sc = new Scanner(System.in);
                //Insert Apple
                System.out.print("Enter the number of apple to buy: ");
                int AppleAmount = sc.nextInt();
                //Insert Mango
                System.out.print("Enter the number of mango to buy: ");
                int MangoAmount = sc.nextInt();
                //Default Weight
                int AppleWeight = 103;
                int MangoWeight = 110;
                //TotalWeight
                int TotalWeight = (AppleAmount * AppleWeight) + (MangoAmount * MangoWeight);
                //Calculation
                int G100 = TotalWeight / 100;
                int G50 = TotalWeight %100 /50;
                int G20 = TotalWeight %100 /20;
                int G10 = TotalWeight %100 %20 /10;
                int G5 = TotalWeight %100 %20 %10 /5 ;
                int G1 = TotalWeight %10 %20 %10 %5 /1;
                System.out.println("100g-weight:" + G100);
                System.out.print(" ");
                System.out.println("50g-weight:" + G50);
                System.out.print(" ");
                System.out.println("20g-weight:" + G20);
                System.out.print(" ");
                System.out.println("10g-weight:" + G10);
                System.out.print(" ");
                System.out.println("5g-weight:" + G5);
                System.out.print(" ");
                System.out.println("1g-weight:" + G1);
                System.out.print(" ");
        }
}

Java QuadraticCalculator


//Import Scanner
import java.util.Scanner;
public class QuadraticCalculator
{
        public static void main(String[] args)
        {
             //Title
             System.out.println("Welcome to use Quadratic Calculator ");
                //Scanner
                Scanner sc = new Scanner(System.in);
                //Insert Values  
                System.out.print("Pleast enter the value a: ");
                double ValueA = sc.nextDouble();
                System.out.print("Pleast enter the value b: ");
                double ValueB = sc.nextDouble();
                System.out.print("Pleast enter the value c: ");
                double ValueC = sc.nextDouble();
                //Disciminant
                double Disc = (ValueB * ValueB) - (4 * ValueA * ValueC);
                double QuadFormPlus = (-ValueB + Math.sqrt(Disc)) / (2 * ValueA);
                double QuadFormMinus = (-ValueB - Math.sqrt(Disc)) / (2 * ValueA);
                double QuadFormZero = -ValueB / (2 * ValueA);
                //Result more than zero
                if (Disc > 0) {
                 System.out.println("");
                }
                System.out.println("X1:" + QuadFormPlus);
                System.out.println("X2:" + QuadFormMinus);
                //Result equals zero
                if (Disc == 0) {
                 System.out.println("X:"+ QuadFormZero);
                }
                //Math Error
                if (Disc < 0) {
                 System.out.println("Math Error");
                }
        }
}

Java CaylinderCal



//Import Scanner
import java.util.Scanner;
public class CylinderCal 
{
 public static void main(String[] args)
 {
  //Scanner
  Scanner sc = new Scanner(System.in);
  System.out.print("Please enter the radius: ");
  //insert radius
  double Radius = sc.nextDouble();
        System.out.print("Please enter the height: ");
  //insert height
  double Height = sc.nextDouble();
  //Surface Area
  double Result1 = 2 * Math.PI * Radius * Height;
  System.out.println("The surface area is " + Result1);
  //Volume
  double RadiusSquare = Radius * Radius;
  double Result2 = Math.PI * RadiusSquare * Height;
  System.out.println("The Volume is " + Result2);
 }
}

JAVA SeperateDigit

//Import Scanner
import java.util.Scanner;
public class SeperateDigit
{
 public static void main(String[] args)
 {
  //Scanner
  Scanner sc = new Scanner(System.in);
  System.out.print("Please enter the five numbers interger (00000-99999): ");
  //insert five number interger.
  int Inter = sc.nextInt();
  int Dig1 = Inter / 10000;
  int Dig2 = Inter % 10000 / 1000;
  int Dig3 = Inter % 1000 / 100;
  int Dig4 = Inter % 100 / 10;
  int Dig5 = Inter % 10;   
  //calculation
     System.out.print("Digits in Interger are " + Dig1);
     System.out.print(" " + Dig2);
     System.out.print(" " + Dig3);
     System.out.print(" " + Dig4);
     System.out.print(" " + Dig5);
 }
}