Java Course - Beginner
Java Course - Beginner
Java Course - Beginner
v=Hl-zzrqQoSE&list=PLFE2CE09D83EE3E28
Applet
import java.applet.*;
import java.awt.*;
public class HelloWorldapplet extends Applet
{
public void paint (Graphics g)
{
g.drawString("Hello World!", 50, 25);
}
}
Tutorial 5: Variables
class Apples {
public static void main(String args[]){
double tuna; // declaring a variable
tuna = 5.28; // assigning a variable
System.out.print("I want ");
System.out.print(tuna);
System.out.println(" movies");
System.out.print("Apples"); //finish the line
System.out.println(tuna);
System.out.println(tuna++); //use with five, and than change to six
System.out.println(tuna);
System.out.println(tuna--); //use with seven, and than change to six
System.out.println(tuna);
//assigning operators
tuna = tuna + 5;
//easier way
tuna += 8;
System.out.println(tuna);
tuna -= 8;
tuna *= 8;
<=, >=
girl = 68;
if(boy > 10 || girl < 60){ //&& = AND
|| = OR
System.out.println("You can enter");
}else{
System.out.println("You can not enter");
}
}
}
type in
}
tunaObject.simpleMessage(name);
Method Tuna
public class tuna {
public void simpleMessage(String name){ //name is called argument
System.out.println("Hello " + name);
}
}
Tuna
public class tuna {
private String girlName; // instead of public, public any class can use
available, private only the methods inside this class can manipulate and change
public void setName(String name){
girlName=name;
}
public String getName(){
return girlName;
}
public void saying(){
System.out.printf("Your first gr was %s", getName());
}
}
System.out.println(counter);
counter++;
}while(counter<=10);
}
quadrada)
}
}
Shows:
Intex
0
1
2
3
Value
32
12
18
54
/*HARD
import java.util.Random;
import java.util.Random;
class lalalau {
public static void main (String[] args) {
Random rand = new Random();
int freq[]=new int [7]; //for 0 to 6
for (int roll=1;roll<=100;roll++){
++freq[1+rand.nextInt(6)]; //IMPORTANT 1+ because of this
aspect (0 to 6)
//++freq = each time it hits 1, add 1 to this number, at the
beginin, 0 to 7 start with zero
}
System.out.println("Face\tFrequency");
for(int face=1;face<freq.length;face++){ //count for 1 to 6
System.out.println(face+"\t"+freq[face]);
}
}
}
public static void display(int x[][]){
for(int row=0;row<x.length;row++){
for(int column=0;column<x[row].length;column++){
System.out.print(x[row][column]+"\t");
}
System.out.println();
}
}
}
}
}
import java.util.Scanner;
class Apples {
public static void main (String[] Args){
Scanner dig = new Scanner(System.in);
tuna ABCObject = new tuna();
System.out.println(ABCObject.toMilitary());
System.out.println("Voc deseja setar as horas manualmente? (s/n)");
String letra = dig.nextLine();
switch (letra){
case "s":
ABCObject.Typetime();
break;
case "n":
ABCObject.setTime(13,27,6);
break;
default:
System.out.println("Voce digitou a letra errada sua besta!");
}
System.out.println(ABCObject.toMilitary());
tunaObject.toMilitary());
tunaObject2.toMilitary());
tunaObject3.toMilitary());
tunaObject4.toMilitary());
Tutorial 42 : toString
public class potpie {
private int month;
private int day;
private int year;
public potpie(int m, int d, int y){ //constructor
month = m;
day = d;
year = y;
}
class Apples {
public static void main (String[] Args){
potpie potObject = new potpie(4,5,6);
}
}
Tutorial 43 : Composition
- Referring to objects in othe classes as members
- Using tostring to change an object into a variable string
public class tuna {
private String name;
private potpie birthday; //reference to a potpie object
public tuna(String theName, potpie theDate){
name = theName;
birthday = theDate;
}
public String toString(){
return String.format("My name is %s, my birthday is %s", name,
birthday);
}
}
// object to string
//direciona o objeto para uma classe que o transforma em
string, pronto para compor o texto
Tutorial 44 : Enumeration
public enum tuna { //kind a classes, but using to declare constants = create an
enumeration constructor
bucky("nice","22"),
kelsey("cutie","10"),
julia("bigmistake","12"); //constants and objects
private final String desc; // variables to represent 2 arguments
private final String year;
class Apples {
public static void main (String[] args){
for(tuna people: tuna.values())
System.out.printf("%s\t%s\t%s\n", people,
people.getDesc(), people.getYear());
}
}
Tutorial 46 : Static
public class tuna {
private String first;
private String last;
private static int members = 0; //every object shares the same variable,
change all objects
public tuna(String fn, String ln){
first = fn;
last = ln;
members++;
System.out.printf("Constructor for %s %s, members in the club:
%d\n", first, last, members);
}
} class Apples {
public static void main (String[] args){
tuna member1 = new tuna("Megan","Fox");
tuna member2 = new tuna("Natalie", "Portman");
tuna member3 = new tuna("Taylor", "Swift");
}
}
Tutorial 48 : Final
public class tuna {
private int sum;
private final int NUMBER; //in front of variable, you cant modified no
mather what happens
//must inicialize!
public tuna(int x){
NUMBER = x;
}
public void add(){
sum+=NUMBER;
}
public String toString(){
return String.format("sum = %d\n", sum);
}
} class Apples {
Tutorial 49 : Inheritance
//superclass = because other classes will inherit stuff from here
//instead of modifying all the time the same method in all classes, we create one
method in "food", so all the other classes will inherit
public class food {
public void eat(){
System.out.println("I am the new methods");
}
}
public class potpie extends food{ //extends means inheritance from...
}
public class tuna extends food{
}
class Apples {
public static void main (String[] args){
tuna tunaObject = new tuna();
potpie potObject = new potpie();
tunaObject.eat();
potObject.eat();
}
JTextField item1;
JTextField item2;
JTextField item3;
JPasswordField passwordField;
public tuna(){
super("the title");
setLayout(new FlowLayout());
item1 = new JTextField(10);
add(item1);
item2 = new JTextField("enter text here");
add(item2);
item3 = new JTextField("uneditable", 20);
item3.setEditable(false);
add(item3);
passwordField = new JPasswordField("mypass");
add(passwordField);
//to add some brains to them, waiting on the screen, something to
happen
}
// the class inside othe class inherited all the stuff
private class thehandler implements ActionListener{ //wait events to roll
the code - wait the enter in this case
public void actionPerformed(ActionEvent event){
//it takes one method and its called automately when a event calls
String string = "";
if(event.getSource()==item1)
string=String.format("field 1:
%s",event.getActionCommand()); //get the text from that location
else if(event.getSource()==item2)
string=String.format("field 2:
%s",event.getActionCommand());
else if(event.getSource()==item3)
string=String.format("field 3:
%s",event.getActionCommand());
else if(event.getSource()==passwordField)
string=String.format("password field is : %s",
event.getActionCommand());
}
JOptionPane.showMessageDialog(null,string);
}
}
import javax.swing.JFrame;
class Apples {
public static void main (String[] args){
tuna bucky = new tuna();
bucky.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bucky.setSize(350, 100);
bucky.setVisible(true);
}
bucky.digest(po);
//created an object so we can use all the stuff from fatty
//created new food object because this digest took food object as an
argument
}
}
public class fatty {
public void digest(food x){
x.eat();
}
}
public class food {
void eat(){
}
public class potpie extends food{
void eat(){
System.out.println("This potpie is great");
}
}
}
class Apples {
public static void main (String[] args){
DogList DLO = new DogList();
Dog d = new Dog();
DLO.add(d);
}
}
private int i = 0;
public void add(Animal a){
if(i<thelist.length){
thelist[i]=a;
System.out.println("Animal added at index "+i);
i++;
}
}
}
public class Animal {
}
public class Fish extends Animal{
}
public class Dog extends Animal{
}
import
import
import
import
javax.swing.JButton;
javax.swing.Icon;
javax.swing.ImageIcon;
javax.swing.JOptionPane;
Tutorial 64 JCheckBox
Tutorial 65 The final CheckBox Program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Gui extends JFrame{
private JTextField tf;
go.setVisible(true);
Tutorial 66 JRadioButton
Tutorial 67 JRadioButton Final Program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//radiobuttom kind of checkbox, but you can have one radio buttom selected at the
time
public class Gui extends JFrame{
private JTextField tf;
private Font pf; //plain
private Font bf; //bold
private Font itf; //italic
private Font bif; //bold and italic
private JRadioButton pb;
private JRadioButton bb;
private JRadioButton ib;
private JRadioButton bib;
private ButtonGroup group; //stablish a relationship that only one buttom
can be selectef
public Gui(){
super("The Title");
setLayout(new FlowLayout());
tf = new JTextField("Thiago is awsome and hot", 25);
add(tf);
pb = new JRadioButton("plain",true); //true = checked false =
unchecked
bb = new JRadioButton("bold",false);
ib = new JRadioButton("italic",false);
bib = new JRadioButton("bold and italic",false);
add(pb);
add(bb);
add(ib);
add(bib);
group = new ButtonGroup();
group.add(pb);
group.add(bb);
group.add(ib);
group.add(bib);
pf = new Font("Serif", Font.PLAIN,14);
bf = new Font("Serif", Font.BOLD,14);
itf = new Font("Serif", Font.ITALIC,14);
bif = new Font("Serif", Font.BOLD + Font.ITALIC,14);
tf.setFont(pf);
//wait for event to happen, pass in font object to constructor
pb.addItemListener(new HandlerClass(pf));
bb.addItemListener(new HandlerClass(bf));
ib.addItemListener(new HandlerClass(itf));
bib.addItemListener(new HandlerClass(bif));
}
private class HandlerClass implements ItemListener{
private Font font;
//the font object get variable font
public HandlerClass(Font f){
font = f;
}
//tets the font to the font object that was passed in
public void itemStateChanged(ItemEvent event){
tf.setFont(font);
}
}
}
import javax.swing.JFrame;
class Apples {
public static void main (String[] args){
Tutorial 68 JComboBox
Tutorial 69 Drop Down list program
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//combobox - list of pictures in this exemple
public class Gui extends JFrame{
private JComboBox box;
private JLabel picture;
//2 array, one of filenames, one of images
private static String[] filename = {"b.jpg", "x.jpg"};
private Icon[] pics = {new
ImageIcon(getClass().getResource(filename[0])),new
ImageIcon(getClass().getResource(filename[1]))};
public Gui(){
super("the title");
setLayout(new FlowLayout());
box = new JComboBox(filename);
event){
if(event.getStateChange()==ItemEvent.SELECTED)
picture.setIcon(pics[box.getSelectedIndex()]);
}
}
);
add(box);
picture=new JLabel(pics[0]);
add(picture);
}
Tutorial 70 JList
Tutorial 71 JList Program
import
import
import
import
java.awt.*;
java.awt.event.*;
javax.swing.*;
javax.swing.event.*;
}
}
);
java.awt.*;
java.awt.event.*;
javax.swing.*;
javax.swing.event.*;
//list on left, when you select multiple itens, they move to the right
public class Gui extends JFrame{
private
private
private
private
"morebacon"};
JList leftlist;
JList rightlist;
JButton movebutton;
static String[] foods = {"bacon","wings","ham", "beer",
public Gui(){
super("title");
setLayout(new FlowLayout());
leftlist = new JList (foods);
leftlist.setVisibleRowCount(3);
leftlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(new JScrollPane(leftlist));
event){
rightlist.setListData(leftlist.getSelectedValues());
}
}
);
add(movebutton);
rightlist = new JList();
rightlist.setVisibleRowCount(3);
rightlist.setFixedCellWidth(100);
rightlist.setFixedCellHeight(15);
rightlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
add(new JScrollPane(rightlist));
}
button
else
System.out.println(x.getName()+"exist!");
System.out.println("this file doenst exist");
}
}
file
class Apples {
public static void main (String[] args){
createfile g = new createfile();
g.openFile();
g.addRecords();
g.closeFile();
}
}
public void closeFile(){
x.close();
}
class Apples {
public static void main (String[] args){
readfile r = new readfile();
r.openFile();
r.readFile();
r.closeFile();
}
do{
try{ //if you have an error hier inside the Try, its go to catch
System.out.println("Enter first num: ");
int n1 = input.nextInt();
System.out.println("Enter second num: ");
int n2 = input.nextInt();
int sum =n1/n2;
System.out.println(sum); //if divides by 0, an error will apear
x=2;
}
catch(Exception e){ //exception e catch all the errors
System.out.println("You cant do that");
}
}while(x==1);
}
Tutorial 83 FlowLayout
import java.awt.*; //colors, border lauyout, etc
import java.awt.event.*;
import javax.swing.*;
public class Layout extends JFrame {
private
private
private
private
private
JButton lb;
JButton cb;
JButton rb;
FlowLayout layout;
Container container;
public Layout(){
super("the title");
layout = new FlowLayout();
container = getContentPane();
setLayout (layout);
);
//right stuff in here
rb = new JButton("right");
add(rb);
rb.addActionListener(
new ActionListener(){
public void
actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.RIGHT);
layout.layoutContainer(container);
}
);
//CENTER stuff in here
cb = new JButton("center");
add(cb);
cb.addActionListener(
new ActionListener(){
public void
actionPerformed(ActionEvent event){
layout.setAlignment(FlowLayout.CENTER);
layout.layoutContainer(container);
}
);
}
}
import javax.swing.*;
class bucky {
public static void main (String[] args){
Layout go = new Layout();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(300,100);
go.setVisible(true);
}