Book Image

Java Fundamentals

By : Gazihan Alankus, Rogério Theodoro de Brito, Basheer Ahamed Fazal, Vinicius Isola, Miles Obare
Book Image

Java Fundamentals

By: Gazihan Alankus, Rogério Theodoro de Brito, Basheer Ahamed Fazal, Vinicius Isola, Miles Obare

Overview of this book

Since its inception, Java has stormed the programming world. Its features and functionalities provide developers with the tools needed to write robust cross-platform applications. Java Fundamentals introduces you to these tools and functionalities that will enable you to create Java programs. The book begins with an introduction to the language, its philosophy, and evolution over time, until the latest release. You'll learn how the javac/java tools work and what Java packages are - the way a Java program is usually organized. Once you are comfortable with this, you'll be introduced to advanced concepts of the language, such as control flow keywords. You'll explore object-oriented programming and the part it plays in making Java what it is. In the concluding chapters, you'll get to grips with classes, typecasting, and interfaces, and understand the use of data structures, arrays, strings, handling exceptions, and creating generics. By the end of this book, you will have learned to write programs, automate tasks, and follow advanced courses on algorithms and data structures or explore more advanced Java courses.
Table of Contents (12 chapters)
Java Fundamentals
Preface

Lesson 2: Variables, Data Types, and Operators


Activity 4: Inputting Student Information and Outputting an ID

Solution:

  1. Import the Scanner package and create a new class

    import java.util.Scanner;
    {
    public class Input{
    static Scanner sc = new Scanner(System.in);
        public static void main(String[] args) 
    {
  2. Take the student name as a string.

    System.out.print("Enter student name: ");
    String name = sc.nextLine();
  3. Take the university name as a string.

    System.out.print("Enter Name of the University: ");
    String uni = sc.nextLine();
  4. Take the student's age as an integer.

    System.out.print("Enter Age: ");
    int age = sc.nextInt();
  5. Print out the student details.

    System.out.println("Here is your ID");
    System.out.println("*********************************");
    System.out.println("Name: " + name);
    System.out.println("University: " + uni);
    System.out.println("Age: " + age);
    System.out.println("*********************************");
        }
    } 
    }

Activity 5: Calculating the Number of Full Fruit Boxes

Solution:

  1. Right-click the src folder and select New | Class.

  2. Enter PeachCalculator as the class name, and then click OK.

  3. Import the java.util.Scanner package:

    import java.util.Scanner;
  4. In the main() enter the following:

    public class PeachCalculator{
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.print("Enter the number of peaches picked: ");
        int numberOfPeaches = sc.nextInt();
        int numberOfFullBoxes = numberOfPeaches / 20;
        int numberOfPeachesLeft = numberOfPeaches - numberOfFullBoxes * 20;
        System.out.printf("We have %d full boxes and %d peaches left.", numberOfFullBoxes, numberOfPeachesLeft);
    }
    }
  5. Run the main program.

    The output should be similar to:

    Enter the number of peaches picked: 55
    We have 2 full boxes and 15 peaches left.