Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
d339716502 |
Binary file not shown.
Before Width: | Height: | Size: 84 KiB |
@ -100,56 +100,6 @@ public class Basics {
|
|||||||
System.out.println("Something else!");
|
System.out.println("Something else!");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
Given the age of a person as an input, output their age group.
|
|
||||||
Here are the age groups you need to handle:
|
|
||||||
Child: 0 to 11
|
|
||||||
Teen: 12 to 17
|
|
||||||
Adult: 18 to 64
|
|
||||||
Sample Input
|
|
||||||
42
|
|
||||||
Sample Output
|
|
||||||
Adult
|
|
||||||
*/
|
|
||||||
int ages = sc.nextInt();
|
|
||||||
if (ages > 0 && ages <= 11) {
|
|
||||||
System.out.println("Child");
|
|
||||||
}else if(ages <= 17){
|
|
||||||
System.out.println("Teen");
|
|
||||||
} else if (ages <= 64) {
|
|
||||||
System.out.println("Adult");
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Your math teacher asked you to calculate the sum of the numbers 1 to N, where N is a given number.
|
|
||||||
Task: Take an integer N from input and output the sum of the numbers 1 to N, inclusive.
|
|
||||||
Sample Input:
|
|
||||||
10
|
|
||||||
Sample Output:
|
|
||||||
55
|
|
||||||
*/
|
|
||||||
|
|
||||||
int number = sc.nextInt();
|
|
||||||
int sum = 0;
|
|
||||||
while (number > 0) {
|
|
||||||
sum += number;
|
|
||||||
number--;
|
|
||||||
}
|
|
||||||
System.out.println(sum);
|
|
||||||
|
|
||||||
/*
|
|
||||||
The factorial of a number N is equal to 1 * 2 * 3 * ... * N
|
|
||||||
For example, the factorial of 5 is 1* 2 * 3 * 4 * 5 = 120.
|
|
||||||
Create a program that takes a number from input and output the factorial of that number.
|
|
||||||
*/
|
|
||||||
long fact = 1;
|
|
||||||
int num = sc.nextInt();
|
|
||||||
for (int i = 2; i <= num; i++) {
|
|
||||||
fact *= i;
|
|
||||||
}
|
|
||||||
System.out.println(fact);
|
|
||||||
|
|
||||||
sc.close();
|
sc.close();
|
||||||
sci.close();
|
sci.close();
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
BIN
Basics/target/classes/com/example/Main.class
Normal file
BIN
Basics/target/classes/com/example/Main.class
Normal file
Binary file not shown.
@ -1,16 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<groupId>com.example</groupId>
|
|
||||||
<artifactId>second</artifactId>
|
|
||||||
<version>1.0-SNAPSHOT</version>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<maven.compiler.source>17</maven.compiler.source>
|
|
||||||
<maven.compiler.target>17</maven.compiler.target>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
</project>
|
|
@ -1,140 +0,0 @@
|
|||||||
package com.example;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class BasicII {
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
System.out.println("Hello world!");
|
|
||||||
|
|
||||||
int [] ages;
|
|
||||||
ages = new int[10];
|
|
||||||
int [] numbers = {15, 36, 18, 24, 47, 59, 26};
|
|
||||||
ages[2] = 15;
|
|
||||||
ages[0] = 18;
|
|
||||||
System.out.println(numbers[3]);
|
|
||||||
|
|
||||||
/*
|
|
||||||
You are making a program for a vending machine that provides drinks.
|
|
||||||
The menu of the drinks is stored in an array called menu:
|
|
||||||
String[] menu = {"Tea", "Espresso", "Americano", "Water", "Hot Chocolate"};
|
|
||||||
JAVA
|
|
||||||
Take the choice of the customer as an integer from input and output the corresponding menu item.
|
|
||||||
Also, check for errors: in case the input is out of the range of the array, output "Invalid".
|
|
||||||
*/
|
|
||||||
String[] menu = {"Tea", "Espresso", "Americano", "Water", "Hot Chocolate"};
|
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
System.out.print("Your chouse from the vending machine [0-4]: ");
|
|
||||||
int choice = sc.nextInt();
|
|
||||||
if (choice > 4 || choice < 0) {
|
|
||||||
System.out.println("Invalid");
|
|
||||||
}else{
|
|
||||||
System.out.println(menu[choice]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
The given code declares an array that holds the monthly revenues for a company for a year.
|
|
||||||
You need to calculate the average monthly revenue for the year.
|
|
||||||
For that, calculate the sum of the revenue for all the months and divide it by the number of items in the array.
|
|
||||||
*/
|
|
||||||
double[] revenue = {88750, 125430, 99700, 14500, 158000, 65000, 99000, 189000, 210000, 42000, 165800, 258900};
|
|
||||||
double sum = 0;
|
|
||||||
for (double d : revenue) {
|
|
||||||
sum += d;
|
|
||||||
}
|
|
||||||
System.out.println(sum/revenue.length);
|
|
||||||
|
|
||||||
//Multidimensional arrays (row, col)
|
|
||||||
int[ ][ ] sample = { {1, 2, 3}, {4, 5, 6} };
|
|
||||||
System.out.println(sample[0][2]);
|
|
||||||
|
|
||||||
/*
|
|
||||||
You are creating a ticketing program for a small movie theater.
|
|
||||||
The seats are represented using a 2-dimensional array.
|
|
||||||
Each item can have the values 1 and 0 - 1 is occupied, and 0 if it's free.
|
|
||||||
Your program needs to take as input the row and the column of the seat and output Free if it's free, and Sold if it's not.
|
|
||||||
*/
|
|
||||||
int[][] seats = {
|
|
||||||
{0, 0, 0, 1, 1, 1, 0, 0, 1, 1},
|
|
||||||
{1, 1, 0, 1, 0, 1, 1, 0, 0, 0},
|
|
||||||
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
|
|
||||||
{0, 0, 0, 1, 1, 1, 1, 0, 0, 0},
|
|
||||||
{0, 1, 1, 1, 0, 0, 0, 1, 1, 1}
|
|
||||||
};
|
|
||||||
System.out.print("The row you want: ");
|
|
||||||
int row = sc.nextInt();
|
|
||||||
System.out.print("The column you want: ");
|
|
||||||
int col = sc.nextInt();
|
|
||||||
|
|
||||||
if (seats[row][col] > 0) {
|
|
||||||
System.out.println("Sold");
|
|
||||||
}else{
|
|
||||||
System.out.println("Free");
|
|
||||||
}
|
|
||||||
|
|
||||||
Welcome();
|
|
||||||
|
|
||||||
/*
|
|
||||||
You are making an automated response program for a store.
|
|
||||||
The bot should take a number from the user as input and reply with an automated message.
|
|
||||||
There are currently 3 responses, that you need to a handle:
|
|
||||||
User message: "1", Reply: "Order confirmed"
|
|
||||||
User message: "2", Reply: "info@sololearn.com"
|
|
||||||
For any other number, the reply should be: "Try again".
|
|
||||||
The given code calls a method called bot(). Define the method, which should take an integer input from the user, and handle the above mentioned cases, by outputting the corresponding reply.
|
|
||||||
Do not change the method call in main().
|
|
||||||
*/
|
|
||||||
bot();
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
You need to make a method that converts a foot value to inches.
|
|
||||||
1 foot has 12 inches.
|
|
||||||
Define a convert() method, that takes the foot value as its argument and outputs the inches value.
|
|
||||||
The result must be a double.
|
|
||||||
*/
|
|
||||||
double num = sc.nextDouble();
|
|
||||||
convert(num);
|
|
||||||
|
|
||||||
/*
|
|
||||||
You are making a Celsius to Fahrenheit converter.
|
|
||||||
Write a method to take the Celsius value as an argument and return the corresponding Fahrenheit value.
|
|
||||||
Sample Input
|
|
||||||
36
|
|
||||||
Sample Output
|
|
||||||
96.8
|
|
||||||
The given code takes the celsius value as input and passes it to a fahr() method, which you need to create.
|
|
||||||
*/
|
|
||||||
double c = sc.nextDouble();
|
|
||||||
System.out.println(fahr(c));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static double fahr(double cels){
|
|
||||||
return 1.8*cels+32;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void convert(double nm){
|
|
||||||
System.out.println(nm*12);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void Welcome(){
|
|
||||||
System.out.println("Welcome!!");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void bot(){
|
|
||||||
Scanner sc = new Scanner(System.in);
|
|
||||||
int num = sc.nextInt();
|
|
||||||
|
|
||||||
switch (num) {
|
|
||||||
case 1:
|
|
||||||
System.out.println("Order confirmed");
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
System.out.println("info@sololearn.com");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
System.out.println("Try again");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Binary file not shown.
0
Basics_II/test.txt
Normal file
0
Basics_II/test.txt
Normal file
@ -1 +0,0 @@
|
|||||||
https://www.sololearn.com/certificates/CC-WHOYREFK
|
|
@ -1,2 +0,0 @@
|
|||||||
In this repository I've completed the Introduction to Java course of SOLOLEARN.
|
|
||||||
The official URL of the site: https://www.sololearn.com/en/learn/courses/java-introduction
|
|
Binary file not shown.
Reference in New Issue
Block a user