Skip to content

ha308ing/n_java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java Intro Examples

from

Main (Garage)

javac -cp "./w3schools;./w3schools/garage/mypackage" -d "./build/w3schools" Main.java
java -cp "./build/w3schools" Main
  1. Asks for data:
    • number of spots
    • person name
    • car brand, color
    • garage spot
  2. Prints inputs

Used: ArrayList, inheritance, abstract class, Scanner, loop, for .. in

Classes:

  • Main
  • Person
  • Vehicle
  • Car
  • GarageSpot
  • Garage
  • Time
  • mypackage

  • Main
    • imports local package
    • use Scanner for input
    • use
  • Car extends abstract class Vehicle
    • brand
    • color
  • Person
    • name
  • GarageSpot
    • car: Car
    • owner: _Person
    • spotNumber
  • Garage: array of GarageSpot's
  • Time
    • imports
      • java.time.LocalDateTime
      • java.time.format.DateTimeFormatter
    • returns formatted now() string

File Handling

javac -d "./build/w3schools/fileHandling" "./w3schools/FileHandling.java"
java -cp "./build/w3schools/fileHandling" FileHandling
  • Creates a file
  • Writes to the file
  • Reads from the file
  • Gets file info
  • Deletes the file

imports:

  • java.io.File
  • java.io.FileWriter
  • java.io.IOException
  • java.util.Scanner

Goods

javac -d "./build/w3schools/goods" "./w3schools/Goods.java"
java -cp "./build/w3schools/goods" Goods
  • Creates a HashSet
  • Add items to the HashSet
  • Removes an item
  • Iterates through the HashSet and removes an item with an iterator

imports:

  • java.util.Iterator
  • java.util.HashSet

Numbers

javac -d "./build/w3schools/numbers" "./w3schools/Numbers.java"
java -cp "./build/w3schools/numbers" Numbers
  • Asks for the array size
  • Gets the array items with Scanner(System.in)
  • Sorts the array with Collections.sort
  • Prints original and sorted array with a static method

imports:

  • java.util.Scanner
  • java.util.ArrayList
  • java.util.Collections
  • java.util.function.Consumer

PrintFormatted

javac -d "./build/w3schools/printFormatted" "./w3schools/PrintFormatted.java"
java -cp "./build/w3schools/printFormatted" PrintFormatted

Use lamda for a string transformation and output

StringSearch

javac StringSearch.java
java StringSearch

User a regular expression for string search

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages