import java.util.Scanner; public class HelloWorld { Scanner input = new Scanner(System.in); public static void main(String[] args) { new HelloWorld(); } // This will act as our program switchboard public HelloWorld() { String[] cargohold = new String[10]; System.out.println("Welcome to the BlackStar Cargo Hold interface."); System.out.println("Please select a number from the options below"); System.out.println(""); while (true) { // Give the user a list of their options System.out.println("1: Add an item to the cargo hold."); System.out.println("2: Remove an item from the cargo hold."); System.out.println("3: Sort the contents of the cargo hold."); System.out.println("4: Search for an item."); System.out.println("5: Display the items in the cargo hold."); System.out.println("0: Exit the BlackStar Cargo Hold interface."); // Get the user input int userChoice = input.nextInt(); input.nextLine(); switch (userChoice) { case 1: addItem(cargohold); break; case 2: removeItem(cargohold); break; case 3: sortItems(cargohold); break; case 4: searchItems(cargohold); break; case 5: displayItems(cargohold); break; case 0: System.out.println("Thank you for using the BlackStar Cargo Hold interface. See you again soon!"); System.exit(0); } } } private void addItem(String cargohold[]) { String additem= input.next(); for(int i=0; i< cargohold.length;i++){ if(cargohold[i]==null){ cargohold[i]=additem; i=cargohold.length; } if(i==cargohold.length-1 && cargohold[cargohold.length-1]!=additem){ System.out.println("Sorry your inventory is full."); } } System.out.println("You have added "+additem); } private void removeItem(String cargohold[]) { System.out.println("Specify the item you want to remove."); String removeitem= input.next(); for(int i=cargohold.length-1;i>-1;i--){ if(cargohold[i]==removeitem){ cargohold[i]=null; i=0; } if(i==0 && cargohold[0]!= removeitem){ System.out.println("That Item couldn't be found in your inventory."); } } } private void sortItems(String cargohold[]) { if(cargohold[0] !=) for(int i=1; i<cargohold.length; i++){ int key= cargohold[i]; int j= i-1; while(j>=0 & cargohold[j].compareTo(key)>=0){ cargohold[j+1]=cargohold[j]; j=j-1; } cargohold[j+1]=key; } System.out.println("List has been sorted."); System.out.println("Would you like to view the sorted list?: 1: Yes 2: No"); int answer=input.nextInt(); if(answer==1){ for(int i=0; i< cargohold.length; i++){ System.out.println(i+" | "+cargohold[i]); if(cargohold[i+1]==null){ i=cargohold.length; } } } } private void searchItems(String cargohold[]) { System.out.println("What item do you want to find?"); String item= input.next(); int Point=0; int[] itemlocations=int[cargohold.lenght]; for(int i=0; i<cargohold.length;i++){ if(cargohold[i]==item){ itemlocations[Point]=i; Point=i+1; } } if(Point==0){ System.out.println("Couldn't find the item."); } else{ System.out.println("We have found "+item+" at: ") for(int k=0; k<itemlocations.length; k++){ System.out.print(itemlocations[k]+","); if(itemlocations[k+1]==null){ k=itemlocations.length; } } System.out.print(" positions in your inventory."); } } private void displayItems(String cargohold[]) { int[] counter=int[cargohold.length]; String[] uniqueItems= String[cargohold.length]; int firstnull=0; for(int i=0; i<cargohold.length;i++){ if(i=0){ uniqueItems[0]=cargohold[0]; counter[0]++; firstnull=1; } else{ for(int j=0; j<i; j++){ if(uniqueItems[j]=cargohold[i]){ counter[j]++; } else{ uniqueItems[firstnull]=cargohold[i]; counter[firstnull]++; firstnull++; } } } } System.out.println("You have: "); for(int i=0; i<uniqueItems.length; i++){ System.out.println(uniqueItems[i]+" - "+counter[i]); } System.out.println("In your inventory"); } }
public class HelloWorld{ public static void main(String []args){ System.out.println( "Hello World."); } }
public class HelloWorld{ public static void main(String []args){ System.out.println("Hello World"); } }
import java.io.*; import java.util.*; public class HelloWorld{ public static void main(String []args)throws IOException { Scanner sc = new Scanner(new File("test.txt")); String str=""; while(sc.hasNext()) { String s = sc.next(); str+=s; str+=" "; } HashMap<String, Integer> map = new HashMap<>(); String sentence = str.toLowerCase(); for(String word : sentence.split("\\W")) { if(word.isEmpty()) continue; if(map.containsKey(word)) map.put(word, map.get(word)+1); else map.put(word, 1); } for(Map.Entry<String, Integer> entry : map.entrySet()) System.out.println(entry.getKey() + ": " + entry.getValue()); } }
public class HelloWorld{ public static void main(String []args){ System.out.println("Hello World"); } }
public class HelloWord { int puppyAge; public HelloWord(String name) { // This constructor has one parameter, name. System.out.println("Name chosen is :" + name ); } public void setAge( int age ) { puppyAge = age; } public int getAge( ) { System.out.println("Puppy's age is :" + puppyAge ); return puppyAge; } public static void main(String []args) { /* Object creation */ HelloWord myPuppy = new Puppy( "tommy" ); /* Call class method to set puppy's age */ myPuppy.setAge( 2 ); /* Call another class method to get puppy's age */ myPuppy.getAge( ); /* You can access instance variable as follows as well */ System.out.println("Variable Value :" + myPuppy.puppyAge ); } }
import java.util.*; public class Question4_3 { static class Node { Integer data; Node left; Node right; public Node(Integer data) { this.data = data; } public int height() { int leftHeight = left != null ? left.height() : 0; int rightHeight = right != null ? right.height() : 0; return 1 + Math.max(leftHeight, rightHeight); } public boolean isBalanced() { int leftHeight = left != null ? left.height() : 0; int rightHeight = right != null ? right.height() : 0; return (Math.abs(leftHeight - rightHeight) <= 1); } } public static Node minimalBST(int[] sorted) { return minimalBST(sorted, 0, sorted.length - 1); } private static Node minimalBST(int[] sorted, int start, int end) { if (start > end) { return null; } int mid = (start + end) / 2; Node node = new Node(sorted[mid]); node.left = minimalBST(sorted, start, mid - 1); node.right = minimalBST(sorted, mid + 1, end); return node; } public static Node insert(Node node, Integer data) { if (data == null) { return null; } if (node == null) { node = new Node(data); return node; } if (data.compareTo(node.data) <= 0) { if (node.left == null) { node.left = new Node(data); } else { insert(node.left, data); } } else { if (node.right == null) { node.right = new Node(data); } else { insert(node.right, data); } } return node; } public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Node root = minimalBST(array); System.out.println("Root? " + root.data); // System.out.println("Created BST? " + root.isBST()); System.out.println("Height: " + root.height()); System.out.println("Is balanced? " + root.isBalanced()); Random random = new Random(); root = null; array = new int[20]; for (int i = 0; i < array.length; i++) { array[i] = random.nextInt(array.length); root = insert(root, array[i]); System.out.print(array[i] + " "); } System.out.println(); // root = minimalBST(array); System.out.println("Root? " + root.data); System.out.println("Height: " + root.height()); System.out.println("Is balanced? " + root.isBalanced()); } }
public class HelloWorld { int age; public void pup(int age){ age = age + 1; System.out.println("My pup's age is: "+ age); } public static void main(String []args){ HelloWorld doggy = new HelloWorld(); doggy.pup(1); } }
//Caixa.java: public class Caixa { //Variaveis de classe //O volume total e compartilhado: public static double volumeTotal = 0; //Atributos especificos: private double altura; private double comprimento; private double largura; //Construtor: public Caixa(double a, double b, double c) { altura = a; comprimento = b; largura = b; volumeTotal = volumeTotal+(altura*comprimento*largura); } //Metodo calcular volume: public double volume() { return altura*comprimento*largura; } }
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more