import java.util.*;
public class GeneralizationsD {
public static void main(String[] args) {
ArrayList<String> sports = new
ArrayList<String>();
sports.add("Football");
sports.add("Boxing");
for(String sport : sports) {
System.out.println(sport);
}
//Major cities and the year they were founded
HashMap<String, Integer> majorCities = new HashMap<String, Integer>();
majorCities.put("New York", 1624);
majorCities.put("London", 43);
majorCities.put("Mexico City", 1521);
majorCities.put("Sao Paulo", 1554);
for (String city : majorCities.keySet()) {
System.out.println(city + " was founded in " + majorCities.get(city));
}
}
}
Quotes help make search much faster. Example: "Practice Makes Perfect"
Thursday, October 29, 2015
Codecademy > Learn Java > Iterating over a HashMap Sample Solution
import java.util.HashMap;
public class RestaurantForEach {
public static void main(String[] args) {
HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();
restaurantMenu.put("Turkey Burger", 13);
restaurantMenu.put("Naan Pizza", 11);
restaurantMenu.put("Cranberry Kale Salad", 10);
System.out.println(restaurantMenu.size());
for (String item : restaurantMenu.keySet()) {
System.out.println("A " + item + " costs " + restaurantMenu.get(item) + " dollars.");
}
}
}
public class RestaurantForEach {
public static void main(String[] args) {
HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();
restaurantMenu.put("Turkey Burger", 13);
restaurantMenu.put("Naan Pizza", 11);
restaurantMenu.put("Cranberry Kale Salad", 10);
System.out.println(restaurantMenu.size());
for (String item : restaurantMenu.keySet()) {
System.out.println("A " + item + " costs " + restaurantMenu.get(item) + " dollars.");
}
}
}
Codecademy > Learn Java > HashMap: Access Sample Solution
import java.util.HashMap;
public class Restaurant {
public static void main(String[] args) {
HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();
restaurantMenu.put("Turkey Burger", 13);
restaurantMenu.put("Naan Pizza", 11);
restaurantMenu.put("Cranberry Kale Salad", 10);
System.out.println( restaurantMenu.get("Naan Pizza") );
}
}
public class Restaurant {
public static void main(String[] args) {
HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();
restaurantMenu.put("Turkey Burger", 13);
restaurantMenu.put("Naan Pizza", 11);
restaurantMenu.put("Cranberry Kale Salad", 10);
System.out.println( restaurantMenu.get("Naan Pizza") );
}
}
Codecademy > Learn Java > HashMap: Manipulation Sample Solution
import java.util.HashMap;
public class Restaurant {
public static void main(String[] args) {
HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();
restaurantMenu.put("Turkey Burger", 13);
restaurantMenu.put("Naan Pizza", 11);
restaurantMenu.put("Cranberry Kale Salad", 10);
}
}
public class Restaurant {
public static void main(String[] args) {
HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();
restaurantMenu.put("Turkey Burger", 13);
restaurantMenu.put("Naan Pizza", 11);
restaurantMenu.put("Cranberry Kale Salad", 10);
}
}
Codecademy > Learn Java > HashMap Sample Solution
import java.util.HashMap;
public class Restaurant {
public static void main(String[] args) {
HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();
}
}
public class Restaurant {
public static void main(String[] args) {
HashMap<String, Integer> restaurantMenu = new HashMap<String, Integer>();
}
}
Codecademy > Learn Java > For Each Loop Sample Solution
import java.util.ArrayList;
public class TemperaturesForEach {
public static void main(String[] args) {
ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
weeklyTemperatures.add(78);
weeklyTemperatures.add(67);
weeklyTemperatures.add(89);
weeklyTemperatures.add(94);
for (Integer temperature : weeklyTemperatures) {
System.out.println(temperature);
}
}
}
public class TemperaturesForEach {
public static void main(String[] args) {
ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
weeklyTemperatures.add(78);
weeklyTemperatures.add(67);
weeklyTemperatures.add(89);
weeklyTemperatures.add(94);
for (Integer temperature : weeklyTemperatures) {
System.out.println(temperature);
}
}
}
Codecademy > Learn Java > Iterating over an ArrayList Sample Solution
import java.util.ArrayList;
public class TemperaturesC {
public static void main(String[] args) {
ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
weeklyTemperatures.add(78);
weeklyTemperatures.add(67);
weeklyTemperatures.add(89);
weeklyTemperatures.add(94);
weeklyTemperatures.add(2, 111);
for (int j = 0; j < weeklyTemperatures.size(); j++) {
System.out.println( weeklyTemperatures.get(j) );
}
}
}
public class TemperaturesC {
public static void main(String[] args) {
ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
weeklyTemperatures.add(78);
weeklyTemperatures.add(67);
weeklyTemperatures.add(89);
weeklyTemperatures.add(94);
weeklyTemperatures.add(2, 111);
for (int j = 0; j < weeklyTemperatures.size(); j++) {
System.out.println( weeklyTemperatures.get(j) );
}
}
}
Codecademy > Learn Java > ArrayList: Insertion Sample Solution
import java.util.ArrayList;
public class TemperaturesB {
public static void main(String[] args) {
ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
weeklyTemperatures.add(78);
weeklyTemperatures.add(67);
weeklyTemperatures.add(89);
weeklyTemperatures.add(94);
weeklyTemperatures.add(2, 111);
System.out.println(weeklyTemperatures.get(3));
}
}
public class TemperaturesB {
public static void main(String[] args) {
ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
weeklyTemperatures.add(78);
weeklyTemperatures.add(67);
weeklyTemperatures.add(89);
weeklyTemperatures.add(94);
weeklyTemperatures.add(2, 111);
System.out.println(weeklyTemperatures.get(3));
}
}
Codecademy > Learn Java > ArrayList: Access Sample Solution
import java.util.ArrayList;
public class Temperatures {
public static void main(String[] args) {
ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
weeklyTemperatures.add(78);
weeklyTemperatures.add(67);
weeklyTemperatures.add(89);
weeklyTemperatures.add(94);
System.out.println(weeklyTemperatures.get(1));
}
}
public class Temperatures {
public static void main(String[] args) {
ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
weeklyTemperatures.add(78);
weeklyTemperatures.add(67);
weeklyTemperatures.add(89);
weeklyTemperatures.add(94);
System.out.println(weeklyTemperatures.get(1));
}
}
Codecademy > Learn Java > ArrayList: Manipulation Sample Solution
import java.util.ArrayList;
public class Temperatures {
public static void main(String[] args) {
ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
weeklyTemperatures.add(78);
weeklyTemperatures.add(67);
weeklyTemperatures.add(89);
weeklyTemperatures.add(94);
}
}
public class Temperatures {
public static void main(String[] args) {
ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
weeklyTemperatures.add(78);
weeklyTemperatures.add(67);
weeklyTemperatures.add(89);
weeklyTemperatures.add(94);
}
}
Codecademy > Learn Java > ArrayList Sample Solution
import java.util.ArrayList;
public class Temperatures {
public static void main(String[] args) {
ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
}
}
public class Temperatures {
public static void main(String[] args) {
ArrayList<Integer> weeklyTemperatures = new ArrayList<Integer>();
}
}
Codecademy > Learn Java > For Loop Sample Solution
public class For {
public static void main(String[] args) {
for (int waterLevel = 0; waterLevel < 7; waterLevel++) {
System.out.println("The pool's water level is at " + waterLevel + " feet.");
}
}
}
public static void main(String[] args) {
for (int waterLevel = 0; waterLevel < 7; waterLevel++) {
System.out.println("The pool's water level is at " + waterLevel + " feet.");
}
}
}
Codecademy > Learn Java > Generalizations Coffee.java Sample Solution
class Coffee extends Beverage{
public Coffee() {
}
public void addSugar(int cubes) {
System.out.println("You added " + cubes + " sugar cubes.");
}
public static void main(String[] args) {
Coffee myOrder = new Coffee();
myOrder.addSugar(2);
myOrder.isFull();
}
}
public Coffee() {
}
public void addSugar(int cubes) {
System.out.println("You added " + cubes + " sugar cubes.");
}
public static void main(String[] args) {
Coffee myOrder = new Coffee();
myOrder.addSugar(2);
myOrder.isFull();
}
}
Codecademy > Learn Java > Inheritance Sample Solution
class Dog extends Animal{
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public void bark() {
System.out.println("Woof!");
}
public void run(int feet) {
System.out.println("Your dog ran " + feet + " feet!");
}
public int getAge() {
return age;
}
public static void main(String[] args) {
Dog spike = new Dog(1);
spike.bark();
spike.run(100);
int spikeAge = spike.getAge();
System.out.println(spikeAge);
spike.checkStatus();
}
}
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public void bark() {
System.out.println("Woof!");
}
public void run(int feet) {
System.out.println("Your dog ran " + feet + " feet!");
}
public int getAge() {
return age;
}
public static void main(String[] args) {
Dog spike = new Dog(1);
spike.bark();
spike.run(100);
int spikeAge = spike.getAge();
System.out.println(spikeAge);
spike.checkStatus();
}
}
Codecademy > Learn Java > Using Methods: II Sample Solution
class Dog{
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public void bark() {
System.out.println("Woof!");
}
public void run(int feet) {
System.out.println("Your dog ran " + feet + " feet!");
}
public int getAge() {
return age;
}
public static void main(String[] args) {
Dog spike = new Dog(1);
spike.bark();
spike.run(100);
int spikeAge = spike.getAge();
System.out.println(spikeAge);
}
}
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public void bark() {
System.out.println("Woof!");
}
public void run(int feet) {
System.out.println("Your dog ran " + feet + " feet!");
}
public int getAge() {
return age;
}
public static void main(String[] args) {
Dog spike = new Dog(1);
spike.bark();
spike.run(100);
int spikeAge = spike.getAge();
System.out.println(spikeAge);
}
}
Codecademy > Learn Java > Methods: II Sample Solution
class Dog{
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public void bark() {
System.out.println("Woof!");
}
public void run(int feet) {
System.out.println("Your dog ran " + feet + " feet!");
}
public static void main(String[] args) {
Dog spike = new Dog(1);
spike.bark();
spike.run(100);
}
}
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public void bark() {
System.out.println("Woof!");
}
public void run(int feet) {
System.out.println("Your dog ran " + feet + " feet!");
}
public static void main(String[] args) {
Dog spike = new Dog(1);
spike.bark();
spike.run(100);
}
}
Codecademy > Learn Java > Using Methods: I Sample Solution
class Dog{
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public void bark() {
System.out.println("Woof!");
}
public static void main(String[] args) {
Dog spike = new Dog(1);
spike.bark();
}
}
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public void bark() {
System.out.println("Woof!");
}
public static void main(String[] args) {
Dog spike = new Dog(1);
spike.bark();
}
}
Codecademy > Learn Java > Methods: I Sample Solution
class Dog{
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public void bark() {
System.out.println("Woof!");
}
public static void main(String[] args) {
Dog spike = new Dog(1);
}
}
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public void bark() {
System.out.println("Woof!");
}
public static void main(String[] args) {
Dog spike = new Dog(1);
}
}
Codecademy > Learn Java > Objects Sample Solution
class Dog{
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public static void main(String[] args) {
Dog spike = new Dog(1);
}
}
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public static void main(String[] args) {
Dog spike = new Dog(1);
}
}
Codecademy > Learn Java > Classes: Constructor Parameters Sample Solution
class Dog{
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public static void main(String[] args) {
}
}
int age;
public Dog(int dogsAge){
age = dogsAge;
}
public static void main(String[] args) {
}
}
Codecademy > Learn Java > Classes: Instance Variables Sample Solution
class Dog{
int age;
public Dog(){
}
public static void main(String[] args) {
}
}
int age;
public Dog(){
}
public static void main(String[] args) {
}
}
Codecademy > Learn Java > Classes: Constructors Sample Solution
class Dog{
public Dog(){
}
public static void main(String[] args) {
}
}
public Dog(){
}
public static void main(String[] args) {
}
}
Codecademy > Learn Java > Classes: Syntax Sample Solution
class Dog{
public static void main(String[] args) {
}
}
public static void main(String[] args) {
}
}
Codecademy > Learn Java > GeneralizationsB.java Sample Solution
public class GeneralizationsB {
public static void main(String[] args) {
// ( 3 >= 3 && !(true || true) )
boolean tricky = false;
if(2015 > 2016) {
System.out.println("Stuck in the past...");
}else {
System.out.println("Upgraded to the future!");
}
int subwayTrain = 5;
switch (subwayTrain){
case 1 : System.out.println("This is a South Ferry bound train!");
break;
case 5 : System.out.println("This is a Brooklyn bound train!");
break;
case 7 : System.out.println("This is a Queens bound train!");
break;
default:
System.out.println("I'm not sure where that train goes...");
}
}
}
public static void main(String[] args) {
// ( 3 >= 3 && !(true || true) )
boolean tricky = false;
if(2015 > 2016) {
System.out.println("Stuck in the past...");
}else {
System.out.println("Upgraded to the future!");
}
int subwayTrain = 5;
switch (subwayTrain){
case 1 : System.out.println("This is a South Ferry bound train!");
break;
case 5 : System.out.println("This is a Brooklyn bound train!");
break;
case 7 : System.out.println("This is a Queens bound train!");
break;
default:
System.out.println("I'm not sure where that train goes...");
}
}
}
Codecademy > Learn Java > Switch Statement Sample Solution
public class Switch {
public static void main(String[] args) {
char penaltyKick = 'L';
switch (penaltyKick) {
case 'L': System.out.println("Messi shoots to the left and scores!");
break;
case 'R': System.out.println("Messi shoots to the right and misses the goal!");
break;
case 'C': System.out.println("Messi shoots down the center, but the keeper blocks it!");
break;
default:
System.out.println("Messi is in position...");
}
}
}
public static void main(String[] args) {
char penaltyKick = 'L';
switch (penaltyKick) {
case 'L': System.out.println("Messi shoots to the left and scores!");
break;
case 'R': System.out.println("Messi shoots to the right and misses the goal!");
break;
case 'C': System.out.println("Messi shoots down the center, but the keeper blocks it!");
break;
default:
System.out.println("Messi is in position...");
}
}
}
Codecademy > Learn Java > Ternary Conditional Sample Solution
public class Ternary {
public static void main(String[] args) {
int fuelLevel = 3;
char canDrive = (fuelLevel > 0) ? 'Y' : 'N';
System.out.println(canDrive);
}
}
public static void main(String[] args) {
int fuelLevel = 3;
char canDrive = (fuelLevel > 0) ? 'Y' : 'N';
System.out.println(canDrive);
}
}
Codecademy > Learn Java > If-ElseIf-Else Statement Sample Solution
public class IfElseIf {
public static void main(String[] args) {
int round = 7;
if (round > 12) {
System.out.println("The match is over!");
} else if (round > 0) {
System.out.println("The match is underway!");
} else {
System.out.println("The boxing match hasn't started yet.");
}
}
}
public static void main(String[] args) {
int round = 7;
if (round > 12) {
System.out.println("The match is over!");
} else if (round > 0) {
System.out.println("The match is underway!");
} else {
System.out.println("The boxing match hasn't started yet.");
}
}
}
Codecademy > Learn Java > If-Else Statement Sample Solution
public class IfElse {
public static void main(String[] args) {
if (7 < 7) {
System.out.println("Try again...");
} else {
System.out.println("Success!");
}
}
}
public static void main(String[] args) {
if (7 < 7) {
System.out.println("Try again...");
} else {
System.out.println("Success!");
}
}
}
Codecademy > Learn Java > If Statement Sample Solution
public class If {
public static void main(String[] args) {
if (true) {
System.out.println("Access granted.");
}
}
}
public static void main(String[] args) {
if (true) {
System.out.println("Access granted.");
}
}
}
Codecademy > Learn Java > Precedence Sample Solution
public class Precedence {
public static void main(String[] args) {
boolean riddle = !( 1 < 8 || (5 > 2 && 3 < 5));
System.out.println(riddle);
}
}
public static void main(String[] args) {
boolean riddle = !( 1 < 8 || (5 > 2 && 3 < 5));
System.out.println(riddle);
}
}
Codecademy > Learn Java > Boolean Operators: ! Sample Solution
public class Not {
public static void main(String[] args) {
System.out.println(!false);
System.out.println( !(5>=1) );
}
}
public static void main(String[] args) {
System.out.println(!false);
System.out.println( !(5>=1) );
}
}
Codecademy > Learn Java > Boolean Operators: || Sample Solution
public class Or {
public static void main(String[] args) {
System.out.println(true || false);
}
}
public static void main(String[] args) {
System.out.println(true || false);
}
}
Codecademy > Learn Java > Boolean Operators: && Sample Solution
public class And {
public static void main(String[] args) {
System.out.println(true && true);
}
}
public static void main(String[] args) {
System.out.println(true && true);
}
}
Codecademy > Learn Java > Generalizations Sample Solution
//Sample Solution
public class Generalizations {
public static void main(String[] args) {
boolean isComplete = true;
int awesomeLevel = 121;
int epicLevel = awesomeLevel * 2;
System.out.println(epicLevel);
}
}
public class Generalizations {
public static void main(String[] args) {
boolean isComplete = true;
int awesomeLevel = 121;
int epicLevel = awesomeLevel * 2;
System.out.println(epicLevel);
}
}
Codecademy > Learn Java > Equality Operators Sample Solution
public class EqualityOperators {
public static void main(String[] args) {
System.out.println(true != false);
}
}
public static void main(String[] args) {
System.out.println(true != false);
}
}
Codecademy > Learn Java > Relational Operators Sample Solution
public class RelationalOperators {
public static void main(String[] args) {
System.out.println(3 < 7);
}
}
public static void main(String[] args) {
System.out.println(3 < 7);
}
}
Codecademy > Learn Java > Math: %
//Sample Solution
public class Modulo {
public static void main(String[] args) {
int myRemainder = 6 % 4;
System.out.println(myRemainder);
}
}
public class Modulo {
public static void main(String[] args) {
int myRemainder = 6 % 4;
System.out.println(myRemainder);
}
}
Codecademy > Learn Java > Math: +, -, *, and /
//Sample Solution
public class Arithmetic {
public static void main(String[] args) {
int myNumber = 3 * 7;
System.out.println(myNumber);
}
}
public class Arithmetic {
public static void main(String[] args) {
int myNumber = 3 * 7;
System.out.println(myNumber);
}
}
Codecademy > Learn Java > Comments Sample Solution
public class Comments {
public static void main(String[] args) {
//System.out.println("Noise!");
/*
anything you like!
*/
}
}
public static void main(String[] args) {
//System.out.println("Noise!");
/*
anything you like!
*/
}
}
Codecademy > Learn Java > Whitespace Sample Solution
public class WhiteSpace {
public static void main(String[] args) {
boolean isFormatted = false;
System.out.println(isFormatted);
}
}
public static void main(String[] args) {
boolean isFormatted = false;
System.out.println(isFormatted);
}
}
Codecademy > Learn Java > Variables Sample Solution
public class Variables {
public static void main(String[] args) {
int myNumber = 42;
boolean isFun = true;
char movieRating = 'A';
}
}
public static void main(String[] args) {
int myNumber = 42;
boolean isFun = true;
char movieRating = 'A';
}
}
Subscribe to:
Posts (Atom)
This is an example of scrolling text using Javascript.
Popular Posts
-
//Sample Solution for MainController.js app.controller('MainController', ['$scope', function($scope) { $scope.title = ...
-
Question: What current is required for full scale deflection of a galvanometer having a current sensitivity of 50 micro-amperes per scale di...
-
Answer: This is the type of injury when a blow to the forehead causes a contusion of the eyeball due to the fracture of the bone at the r...
-
This is caused by the entrance of water into the air sacs which makes the lungs doughy, readily pits on pressure, exuding water and froth
-
var my_canvas=document.getElementById('canvas'); var context=my_canvas.getContext('2d');
-
#Sample Solution --- title: "Data Cleaning in R" output: html_notebook --- ```{r message=FALSE, warning=FALSE} # load libra...
-
# Update data types year_1_str = str(year_1) revenue_1_str = str(revenue_1) # Create a complete sentence combining only the string d...
-
//Sample Solution const gameState = {} function preload() { this.load.image('codey', 'https://s3.amazonaws.com/codecademy-...
-
Question: How do I startup Blend for Visual Studio once it has been installed? Answer: Click on the Windows button at the bottom left co...
-
Instructions: Count the number of rows from the flights table, where arr_time is not null and the destination is ATL. Sample Solution: S...