Java Basics Internship Tasklist
Java Basics Internship Tasklist
In Java, you declare variables using data types that match the kind of value being stored. For a person's name, you would use a String: String name = "Rani";. For age and height, you would use int and double respectively: int age = 23; double height = 5.4;. To display these values, you use the System.out.println() method: System.out.println("My name is " + name); System.out.println("My age is " + age); System.out.println("My height is " + height)
To ensure all user inputs are correctly received for book details in Java, use the Scanner class to read each input separately. Import java.util.Scanner; and create a Scanner object: Scanner input = new Scanner(System.in);. Prompt the user for each item: String title = input.nextLine(); String author = input.nextLine(); int pages = input.nextInt();. Use input.nextLine() for Strings to ensure you read full lines of text, which prevents skipping issues when reading mixed data types. System.out.println() can then display the inputs: System.out.println("Book Title: " + title); System.out.println("Author: " + author); System.out.println("Pages: " + pages)
To output a formatted display in Java, especially for student details, use formatted strings with System.out.printf() or String.format(). For instance: System.out.printf("STUDENT DETAILS \n***********************\nStudent Name: %s\nRoll Number: %d\nDepartment: %s\n", name, rollNumber, department);. This approach allows you to control the format and layout, ensuring consistent spacing and alignment. It's particularly useful for tabulated data and when including variables in a specific pattern.
To input and display a student's information in Java, you can use the Scanner class to read input from the user. First, import java.util.Scanner; and create a Scanner object: Scanner input = new Scanner(System.in);. Then, prompt the user for input and assign it to variables: String name = input.nextLine(); int rollNumber = input.nextInt(); String department = input.next(); Finally, print the information using System.out.println(): System.out.println("Student Name: " + name); System.out.println("Roll Number: " + rollNumber); System.out.println("Department: " + department)
To calculate the sum of three subject marks in Java, declare three integer variables for the marks: int mark1, mark2, mark3;. Then, calculate the total: int total = mark1 + mark2 + mark3;. Display the result using: System.out.println("Total marks: " + total);. When choosing data types, consider the range of values. If marks can be fractional or very large, you might need to use double or long. However, for typical integer marks within a standard range, int is appropriate.
In Java, best practices for naming variables include using descriptive names that convey the variable’s purpose, following camelCase convention, and starting with a lowercase letter. For personal data like name, age, and salary, use names like "personName", "personAge", and "personSalary". These names suggest what data the variable holds, improving readability and maintenance. Avoid abbreviations and ensure names are not too lengthy or complex. Reserve uppercase names for constants .